Tech Punch

Entries from September 2008

WPF / WF – What Is A Dependency Property?

September 25, 2008 · 4 Comments

A new type of property, called a dependency property, was added to the .Net Framework with the release of .Net 3.0.  Dependency properties are used in both Windows Presentation Foundation (WPF) and Workflow Foundations (WF).  They are very similar in both frameworks, but are used for different purposes.

Dependency properties provides the plumbing for property value resolution, change notification, data binding, styling, validation, etc. for properties exposed in Windows Presentation Foundation (WPF) UI elements and Workflow Foundations (WF) custom activities.  Each dependency property is registered with a central repository that handles the change event notifications for you. 

Key Point – The Value of Dependency Properties Are Resolved

The ultimate goal of a dependency property, like any property, is to manage state.  But unlike normal .Net properties, the local property value is not stored in an instance variable. 

Instead, dependency properties are registered with the dependency property framework, and the underlying property value is resolved – meaning the value is determined by the dependency property framework based on rules defined by the property registration.

How To Create A Dependency Property

All WPF UI elements and Workflow Activities are derived from a high-level base classes called DependencyObject, which provides the basic functionality required to implement dependency properties.

public class MySampleControl : Control
{
    // Step 1: Register the dependency property 
    public static readonly DependencyProperty SpecialBrushProperty =
            DependencyProperty.Register("SpecialBrush", typeof(Brush),
            typeof(MySampleControl));

    // Step 2: Provide set/get accessors for the property
    public Brush SpecialBrush
    {
        // IMPORTANT!!
        // -----------
        // Dependency property accessors should not include custom logic 
        // because the framework may call the base.GetValue() and 
        // SetValue() methods directly
        get { return (Brush)base.GetValue(SpecialBrushProperty); }
        set { base.SetValue(SpecialBrushProperty, value); }
    }
}

As show in the code, there are two steps involved in creating a dependency property:

  1. Create a static field to hold a DependencyProperty object
    • By convention, the field should be named with the normal property name, followed by a “Property” suffix
    • This field will not contain the value of the property.  It simply defines the property that is registered with the dependency system
    • This field should be defined as a public, static, read-only field
      • The property must be available at all times, possibly shared among classes
      • The field is defined with the read-only keyword, meaning it can only be set in the static constructor of the type.
    • The field must be instantiated with a call to the static DependencyPropert.Register() method, passing:
      • The name of the property
      • The type of the property 
      • The type of the class that owns the property

      • Optionally, metadata used to define how the property is treated by the WPF framework
        • Meta-data flags to specify how the property affects the layout of the element (AffectsMeasure, AffectsArrange, AffectsRender, etc.)
        • Callbacks for handling property value changes (PropertyChangedCallback)
        • Callbacks to define custom value logic (CoerceValueCallback) 
        • Callbacks to validate values (ValidateValueCallback)
      • Optionally, metadata used to define how the property is treated by the WF framework
        • Include DependencyPropertyOptions enumerated value to specify additional characteristics for the property (ReadOnly, MetaData, etc.)
  2. Create property accessors (get / set)
    • Call DependencyProperty.SetValue() and GetValue() methods to set local values
    • DependencyObject is a high-level framework base class for WPF UI elements and WF activities
    • DependencyObject exposes the GetValue() and SetValue() methods

Isn’t That A Lot of Code for a Property?

At first glance, yes, this looks like an extremely complicated way to declare a property.  OK, at second glance, it’s still pretty complex.  But once you understand the power of dependency properties provide, especially in the Windows Presentation Framework (WPF), they’ll become a welcome addition to your coding toolbox. 

The convention takes a little getting used to. But after you’ve created a few, it comes quite naturally.  Visual Studio also includes some snippets to help add dependency properties:

  • Insert Snippet > NetFX30 > Define a DependencyProperty
  • Insert Snippet > Other > Workflow > DependencyProperty > Property

Subtle Differences Between in WPF and WF

While WPF and WF both use a similar dependency property frameworks, they are not identical!!

WPF UI elements and WF activities derive from a base DependencyObject class, and use a DependencyProperty class to register properties and get/set property values.  Despite the shared names, they are not the same classes!!

  • DependencyObject and DependencyProperty classes used by WPF reside in the System.Windows namespace
  • DependencyObject and DependencyProperty classes used by WF reside in the System.Workflow.ComponentModel namespace

Though their usage and function are very similar, they are not directly related to one another.

How are Dependency Property Values Resolved?

Property values for dependency properties are resolved automatically by the framework according to the following order of precedence:

  1. Property system coercion
  2. Active animations or animations with a hold behavior (WPF)
  3. Local value
  4. TemplateParent template properties (WPF)
  5. Implicit style (WPF)
  6. Style triggers (WPF)
  7. Template triggers (WPF)
  8. Style setters (WPF)
  9. Default (theme) style (WPF)
  10. Inheritance
  11. Default value from dependency property metadata

Note that the local value is 3rd in the order of precedence.  This means that a property may have a value, even if a local value for the property has never been set.  Also, if a property does have a local value, it may be overridden by higher level precedence items like an animation or data binding.

When Should I Use Dependency Properties In WPF?

Dependency properties are one of the most important concepts within WPF.  In WPF, dependency properties are required to support:

  • Data Binding
  • Animation
  • Styling
  • Value expressions
  • Property invalidation
  • Per-type default values

In short, it makes sense to expose dependency properties any time you expose a property for a custom UI element or control. 

When Should I Use Dependency Properties In WF?

Dependency properties are not used as heavily in Workflow Foundations (WF) as they are in Windows Presentation Foundation (WPF) , but they are still an important part of the WF framework.  They are used primarily when developing custom workflow activities that expose properties to support the following scenarios:

  • Activity Binding
    • This is the most common use of dependency properties in WF
    • Allows activities to expose properties that can be used to bind state to another activity
    • For example, binding the input of one workflow activity to the output of another workflow activity
  • Attached Properties
    • Attached properties are used to create a parent activity that can manage the state of it’s child activities
    • For example, the Conditioned Activity Group can attach a When property to each child
  • Meta Properties
    • Meta properties are defined by including new PropertyMetadata(DependencyPropertyOptions.Metadata) in the DependencyProperty.Register() method call 
    • Meta properties are set at design time and cannot be changed at run-time

    • Meta properties exist to guarantee the integrity of an activity
    • For example, at runtime, you can’t change the InterfaceType of an CallExternalMethod activity because InterfaceType is a meta-property

Sources

  del.icio.us it!

digg it!

reddit!

technorati!

yahoo!

Categories: .Net · C# · Visual Studio · WPF · Workflow

WPF – Create Irregular (Non-Rectangular) Borderless Windows

September 19, 2008 · 2 Comments

With Windows Presentation Foundation (WPF), it’s easy to craft irregular, non-rectangular top-level windows without borders to create a unique splash screen or a gadget-style user interface:

  • Modify the Window Properties
    • Set AllowTransparency property to true.
    • Set WindowStyle to None to remove window chrome
    • Set Background to Transparent
  • Add support for moving and closing the window if needed
    • Use the Window’s DragMove() method to allow users to reposition the Window
    • Add a UI element to allow users to close the window
void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    // DragMove handles all the window placement automatically!
    this.DragMove();
}

  del.icio.us it!

digg it!

reddit!

technorati!

yahoo!

Categories: .Net · C# · WPF

SQL Server Reporting Services – Url Parameters in SharePoint Integrated Mode

September 17, 2008 · 16 Comments

SQL Server Reporting Services 2005 and 2008 added a new SharePoint Integration mode, which allows reports to be stored and managed in a SharePoint 2007 portal.  Data connection, reports, security, report subscriptions and notifications are all managed in SharePoint 2007. 

Advantages Of SharePoint Integrated Mode

In SharePoint Integration mode, reports can be rendered using Reporting Services web parts.  These web parts can be added to pages throughout your site.  You can link report web parts to filter web parts, to provide parameters to one or more reports on a page, which allows you to create Reporting Services dashboards.  You can even mix and match them with Excel Services charts, spreadsheets, and pivot tables on a single dashboard page.  Powerful stuff!!

RSViewerPage and the Report Viewer Web Part

When you configure Reporting Services to run in SharePoint Integration Mode, reports are displayed using the /_layouts/ReportServer/RSViewerPage.aspx page by default.  This page provides an a SharePoint look and feel, and integrated navigation back to your SharePoint site.  It leverages the Report Viewer Web Part to display the report.  The Report Viewer Web Part has some very nice features:

  • An integrated SharePoint look/feel
  • Cookie crumb navigation back to your site, MySite, and MyLinks
  • A collapsible parameter panel to the right of the report
  • A nice toolbar with zoom and page navigation
  • An Actions menu on the toolbar, allowing users to:
    • Export to various formats
    • Customize the report using Report Builder
    • Create report subscriptions

SSRSReportViewerWebPart

HTML Viewer in Native Mode and SharePoint Integrated Mode

When you configure Reporting Service to run in Native Mode, reports are displayed using an HTML Viewer interface that is built into Reporting Services.  The HTML Viewer provides all of the basic functionality users required to view, navigate, and print reports. It also provides many Url parameter options to control report parameters and rendering rendering – options that are not available when viewing reports using the RSViewerPage page and the Report Viewer Web Part.

Yes, You Can Use the HTML Viewer in SharePoint Integrated Mode
When you configure Reporting Services to run in SharePoint Integrated Mode, it’s your choice.  You can use either the RSViewerPage or the HTMLViewer to view your reports.  The look and feel of the HTML Viewer isn’t as nice as the RSViewerPage interface, but it’s equally functional, and it provides a lot of Url options that can’t be duplicated using the RSViewerPage.

SSRSHTMLtViewer

In the HTML Viewer, the report parameter options are displayed at the top of the page (the Report Viewer Web Part shows them on the right-hand side of the report).  You can use the HTML Viewer user interface to view the report, change parameters, collapse parameters, zoom, print, export, etc.  When you use the HTML Viewer to view your report, you lose the SharePoint UI look and feel, cookie crumb navigation, MySite and MyLinks integration, and Report Builder and Subscriptions integration.  You gain the Url options we’ll be discussing later in this post – options that are not supported by the RSViewerPage or the Report Viewer Web Part.

How To Display Reports Using the HTML Viewer Page

You still have the ability to call reports using the HTML Viewer page by providing a link to the ReportServer Virtual Directory with a full path to the rdl file, like so…

http://myrsserver/reportserver?http://portal/reports/reportslibrary/regional%20sales.rdl&rs:Command=Render

When you view your report in this manner, you are bypassing the SharePoint Integrated RSViewerPage.aspx page and the web part, and viewing the report using the native mode HTML Viewer.  The rs:Command=Render Url parameter instructs reporting services to display the report. 

Passing Report Parameters in the Url

When you provide a link to the HTML Viewer, you can use the parameter support in the HTML Viewer to control the display of the report.  Assume a report that accepts 3 parameters:

  • Region (integer)
  • StartDate (date)
  • EndDate

By adding Url parameters on the end of the HTML Viewer Url, you can easily pre-populate the report parameters for the user.

http://myrsserver/reportserver?http://portal/reports/sample%20reports/departmental%20sales.rdl&rs:Command=Render&region=2&startdate=9/16/2008&enddate=9/17/2008

As you can see, with this sort of report, you can provide a flexible report that can be used for multiple purposes.  You can easily swap out these links in code to insert the department and dates appropriate in the context of the application displaying the links.  Parameter values in the Url override default parameter values.

If all the parameters are supplied by Url or default parameter settings, the report is rendered automatically, without any further action required from the user.  Nice!!

Passing Report Rendering Instructions in the Url

Using the HTML Viewer, you can also control the rendering of the report.   You can set the rendering format (HTML, PDF, EXCEL, CSV, etc.), the zoom level, and a bookmark or default page displayed in the report. You can also toggle the appearance of toolbars, parameters, and the document map.  You can even provide default search criteria to display and modify the style sheet used to render the report, or set the report to display a specific page.

As an example, the following link will show the report at Whole Page zoom level, with parameters collapsed and document map displayed, with the last page displayed by default.

http://myrsserver/reportserver?http://portal/reports/sample%20reports/departmental%20sales.rdl&rs:Command=Render&rc:Zoom=Whole%20Page&rc:Parameters=collapsed&rc:DocMap=true&rc:Section=999

In the HTML Viewer, there are many options available that can be used to controlling report rendering.  This is a short list of the of the ones you’re likely to find useful:

  • rs:Format – Rendering modes you can pass are HTML3.2, HTML4.0, MHTML, IMAGE, EXCEL, WORD, CSV, PDF, XML, defaults to HTML4.0
  • rc:Zoom – Specified in percentages, supports Page%20Width and Whole%20Page, defaults to 100%
  • rc:Toolbar – True/False, used to show/hide the toolbar, defaults to true
  • rc:Parameters – True/False/Collapesed, used to show/hide/collapse the parameters in the toolbar, defaults to true
  • rc:DocMap – True/False, used to show/hide document map, defaults to true (not shown unless report has document map)
  • rc:Section – Specifies default page number to display, defaults to 1
  • rc:BookMarkID – Jumps to a specific bookmark in a report
  • rc:FindString – Provides search criteria to the report and finds the first instance of the string specified

Links For More Information

For more information on Url parameters available for the Reporting Services HTML Viewer and Report Viewer Web Part, check out these links:


  del.icio.us it! digg it! reddit! technorati! yahoo!

Categories: MOSS 2007 · Reporting Services · SQL Server · SharePoint

Virtual PC 2007 – Installing Vista / Windows Server 2008 Sound Drivers on VPC’s

September 10, 2008 · 8 Comments

When building Windows Vista and Windows Server 2008 VPC’s, I found it is sometimes difficult to get the correct device driver for the sound card installed.  After a few fruitless Google searches, I found lots of people with this problem and few solutions.  I finally found a blog post that gave me the help I needed.  Thanks to Greg Low Bit Bucket for his blog post, Greg Low’s Bit Bucket – Playing videos and sound in Windows Server 2008 using a Virtual PC (VPC).

First, as Greg said, if you’re using Virtual Server, you’re out of luck.  Virtual PC 2007 emulates a sound card, but Virtual Server doesn’t.  I’m not sure about Hyper-V.  If anyone has any findings on Hyper-V sound card emulation, please leave a comment and let us know what you found out.

Virtual PC 2007 Service Pack 1

The service pack has new sound emulator drivers for Vista and Windows Server 2008.  Be sure to install it and setup your VMC as a Windows Vista or Windows Server 2008 virtual machine.  As far as I can tell, the only difference between the operating systems you pick are the sound drivers and the memory allocation.  I normally tweak the memory allocation anyway.

Installing the Sound Drivers

To install the sound drivers, you need to install Virtual Machine Additions first, reboot, and then install the sound drivers from the C:\Program Files\Virtual Machine Additions folder on the VHD.

  1. Install Virtual Machine Additions (File > Install or Update Virtual Machine Additions).  This will mount a virtual drive and run a setup program to install the VM additions.
  2. After installing the software, reboot the VPC as directed
  3. If you get the Found New Hardware dialog, you can point it to the C:\Program Files\Virtual Machine Additions folder to install the drivers from there
  4. If you don’t get the Found New Hardware dialog (you disabled it because it was bugging you to death), you can install the device driver from the Device Manager
    1. Open the Device Manager (Start > Control Panel > Device Manager)
    2. Under the Other devices, you’ll see a yellow icon indicating you have a problem with the Multimedia Audio Controller
    3. Right-click the Multimedia Audio Controller and select the Update Device Driver option
    4. Select the option to “Browse my computer” and point it to the C:\Program Files\Virtual Machine Additions folder
    5. Click Next to install the device drivers
  5. After the device driver setup is complete, your sound should start working, and you’ll no longer have an annoying device driver warning when you startup your VPC

Installing Windows Media Player on Windows Server 2008

Windows Server 2008 does not have Windows Media player installed by default.  If you want to get streaming media audio running, you’ll need to install it.  To install the Media Player, you need to install the “Desktop Experience” feature.

The Desktop Experience feature enables a bunch of stuff that is by default present on a Vista workstation, but is not included by default on a Server 2008 installation.desktop OS.  Most importantly it includes Windows Media player, Themes, and the Aero related features.

  1. Open Server Manager (Start > Administrative Tools > Server Manager)
  2. Select Features > Add Features
  3. Check the Desktop Experience feature and click Next
  4. Click Install to install the feature
  5. Reboot as directed
  6. When the machine comes back up, WIndows Media Player will be there

After installing the Desktop Experience on Windows Server 2008, Windows Media Player will be available.  Themes and Aero will be installed but disabled, but I don’t need them bogging down my VPC anyway.

Sources

Greg Low’s Bit Bucket – Playing videos and sound in Windows Server 2008 using a Virtual PC (VPC)


  del.icio.us it! digg it! reddit! technorati! yahoo!

Categories: Virtual PC · Virtualization · Windows Server 2008 · Windows Vista

SQL Server 2008 – How To Build and Deploy AdventureWorks OLAP Cubes

September 8, 2008 · 5 Comments

I’m trying to setup a simple OLAP demo, using MOSS 2007 Excel Services to display pivot tables and other spreadsheet data, connecting to SQL Server 2008 Analysis Services OLAP cubes.  In this case, I want to get it working with the AdventureWorksDW sample database.  I’m documenting the steps here to help others, and so that I can do it again without the pain!

To complete these steps, you will need SQL Server 2008 installed with the database engine, Analysis Services, FILESTREAM and Full Text Search services enabled.  You’ll also need the Visual Studio Business Intelligence project templates installed to build and deploy your cube using SQL Server Business Intelligence Development Studio (BIDS).

1. Install the AdventureWorksDW Sample Database

First, you’ll need to install the AdventureWorkdsDW sample database from CodePlex.  To install it, you’ll need FILESTREAM and Full Text Search enabled.  For more detailed instructions, see my blog post, SQL Server 2008 – Installing the AdventureWorks Sample Databases.

2. Install and Deploy the AdventureWorks Cubes

After you’ve installed the AdventureWorks DW database, you need to setup a SQL Server Business Intelligence Developer Studio (BIDS) project to create and deploy a cube.  You can find the sample BIDS project located at C:\Program Files\Microsoft SQL Server\100\Tools\Samples\AdventureWorks 2008 Analysis Services Project.  There are 2 projects there, depending on your version of SQL Server you’re using.

  1. Open the sample Adventure Works.sln BI solution using Visual Studio
  2. Change the data connection options for the source database
    1. Expand the solution folders in the Solution Explorer and double-click the Adventure Works.ds data source to open the properties.  This is a data connection to the AdventureWorksDW database, which will serve as the source database for the cubes we’re creating
    2. Edit the connection string and set the connection properties.  I just changed localhost to server name of my database server, using the default Windows Authentication mode.
    3. Click the Test Connection button to test your connection
    4. Exit the configuration dialog to save your changes
  3. Change the data connection options for the Analysis Services database
    1. Select Project > Properties from the Visual Studio menu to open the project options dialog
    2. Select the Deployment options
    3. Change the server to the database server / instance here Analysis Services is installed.  This is where your cube will be created.
    4. By default, the database name is Adventure Works DW 2008.  This is the Analysis Services database that will be created to host the cubes.
    5. Click OK to exit the properties dialog and save your changes
  4. Build the project
    1. Select Build > Build Solution from the Visual Studio menu to build the solution and check for errors.
    2. Select Build > Deploy Solution from the Visual Studio menu to deploy the project.  This will create the cubes and other Bi database objects in the database. 

3. Browse the AdventureWorks Cubes

After your cube has been deployed, you can browse your cube data in the Visual Studio BIDS interface, or in SQL Server Management Studio:

Browse cube data in Visual Studio

  1. Select the AdventureWorks cube in the cubes folder of your BIDS project
  2. Right-click the cube and select Browse from the context menu
  3. Drag/drop measure and dimension data to play with your cube data

Browsing OLAP Cube Using Visual Studio

Browse cube data in SQL Server Management Studio

  1. Open SQL Server Management Studio (Start > Programs > MIcrosoft SQL Server 2008 > SQL Server Management Studio)
  2. Connect to the Analysis Services database
    1. Select File > Connect Object Explorer from the SQL Server Management Studio menu
    2. Select Analysis Services in the Server Type dropdown
    3. Enter the server name and authentication options and click Connect
    4. Expand the Analysis Services server in the object explorer and browse to the Adventure Works DW 2008 > Cubes
    5. Select the AdventureWorks cube, right-click, and select Browse from the context menu
    6. Drag/drop measure and dimension data to play with your cube data, as show below

Browsing OLAP Cube Using SSMS


  del.icio.us it! digg it! reddit! technorati! yahoo!

Categories: Analysis Services · BI · OLAP · Visual Studio