Compartir a través de


VisualStateManager for desktop WPF

As I’ve described before, we introduced the new VisualStateManager concept into Silverlight WPF before we added the same functionality in desktop WPF, in violation of our general guideline that Silverlight is functionally a subset of the desktop .NET Frameworks.  We are planning to add full VSM support on the desktop, but we haven’t had time to support all the existing controls and other infrastructure. 

In the meantime, we would like to enable you to start prototyping desktop controls that use VSM and can therefore share templates and implementation with their Silverlight kindred.  So, for prototyping only purposes, you will find attached to this blog entry the full source code for a desktop WPF implementation of VisualStateManager and a behavior system that allows existing WPF controls to work with it. 

The solution contains a VisualStateManager project that implements VisualStateManager, VisualState, VisualStateGroup and the other classes that VSM requires, and a VSMTestApp project that produces a standalone exe that shows VSM templates applied to some Buttons and a custom WeatherControl. 

Another reason for my blogging this project is that it is a powerful example of the Attached Behavior pattern.  It might seem VisualStateManager needs to be added into the core of WPF in order to function, but as the project shows, it can be implemented through use of an attached property and change callbacks.  To see how this works look in VisualStateManager.cs at the VisualStatesGroupProperty and VisualStateGroupsProperty_Changed callback.

Furthermore, the normal pattern for using Silverlight controls with VisualStateManager is to have the controls call GoToState() in their implementation code.  But desktop WPF controls do not have these GoToState() calls.  The solution is to create VisualStateBehaviors that use the Attached Behavior pattern again to inject the GoToState() calls in response to the proper events.  Take a look in the Behaviors folder of the VisualStateManager project to see how this works.  It includes the abilities to write your own VisualStateBehaviors to add VSM support into any control *without* modifying the control itself. 

I plan to use this project to show how Triggers work with VisualStateManager in the future…stay tuned.

This prototype project is compatible with the VisualStateManager from Silverlight Beta 2.  There is no guarantee this version will be compatible with VSM in the Silverlight RTM and there will be an officially support VSM for desktop WPF in the future.  Please don’t use this code for production purposes, it is purely for experimentation and prototyping now. 

Acknowledgements:  Kenny Young, Kurt Jacob, Dave Relyea Karen Corby and Mike Hillberg all contributed to this project.  Kenny in particular deserves credit for writing the original prototype that led to the features implementation in Silverlight.  Karen Corby ported the custom Weather Control from her blog series on VisualStateManager.  Any bugs in the project were introduced by me.

WpfVSMCode.zip

Comments

  • Anonymous
    August 08, 2008
    PingBack from http://blog.a-foton.ru/2008/08/visualstatemanager-for-desktop-wpf/

  • Anonymous
    August 08, 2008
    Note directly related but, speaking of controls, one thing I'd love to see added to the WPF mix is a Blend-like "draggable" numeric input control, with min, max, steps, format, "progress" etc... just like the one in Blend. Cheers, Axel

  • Anonymous
    August 08, 2008
    While next release of WPF (3.5 SP1) won't yet align with Silverlight's " Parts & States Model

  • Anonymous
    August 08, 2008
    While next release of WPF (3.5 SP1) won't yet align with Silverlight's " Parts & States

  • Anonymous
    August 22, 2008
    Over at JohnGossman's Blog (Tales from the Smart Client) , he has attached quite a neat compilable source project for the latest version of WPF. It's a prototype implementation of the Silverlight Visual State Manager for WPF.. The project includes a neat

  • Anonymous
    August 22, 2008
    Over at JohnGossman's Blog (Tales from the Smart Client) , he has attached quite a neat compilable source project for the latest version of WPF. It's a prototype implementation of the Silverlight Visual State Manager for WPF.. The project includes a neat

  • Anonymous
    September 05, 2008
    John- can you clarify what the strategy is triggers?  Is the VSM a strategic replacement for triggers, or will Silverlight eventually get them and we'll have this as an alternative. Just wanted to know how much time we should spend investing in this as an implementation of Triggers.

  • Anonymous
    September 18, 2008
    The comment has been removed

  • Anonymous
    October 27, 2008
    Well here I am in sunny LA at the PDC. At least I assume it's sunny, not been outside all day. Been too

  • Anonymous
    November 04, 2008
    Now that the Visual State Manager (VSM) is part of the WPFToolKit, I thought I’d show a basic example