WPF– visual states in MvvM

By Mirek on (tags: behaviors, Expression Blend, Visual state, WPF, categories: code)

WPF has many rich features which facilitates making a great look&feel of the application. Unfortunately many of those features are cumbersome to implement and use when we develop in a Model - View – ViewModel architecture. In this post I will try to make you more familiar with visual states and how can we use them in our MvvM application.

Visual state simply defines a state of a control (or more precisely a visual element) by defining its properties values. Additionally in visual states groups we can define a transitions (animations) that are to be performed when switching from one state to another. With use of VisualStateManager we can switch the state of a control from the code behind.
We can define visual states of our control in the XAML or with use of Expression Blend designer. I recommend to use the second option because it speeds up the work rapidly in this case.

Let assume for the sake of simplicity we have a simple window with three buttons. On button will be used to change the state of the window by binding to the attached view model property. The window will have two visual states. In one there will be green button visible and in the second there will be blue button visible. We want to make the transition between visual states to be triggered from the view model.

For the sake of simplicity we will use the window’s code behind class as ViewModel (which is of course not ok with MvvM pattern but makes no difference from the view perspective).

StateSwitch is the property which we want to control the visual state of our application. In XAML we need to set the DataContext of the view and add a binding to the switch button.

 

Ok Now we are ready to define visual states in the application. In Expression Blend select “States” tab and add visual states group and two visual states. Name one GreenState and the second BlueState.

 

Note that when you select a visual state, the designer area is marked with red rectangle and the green state recording is on. It mean that any change to the visual elements on the view is registered for current visual state. Ok, then if in GreenState we want only green button be visible let’s make blue button collapsed here. The same for BlueState.Click on BlueState to make it recording on (both buttons should be visible now) and make green button collapsed this time.

Additionally you can set the Opacity of the button to 0% when it is collapsed. This will allows us to animate the button visibility transition.

Set 1 second in “Default transition”. This will generate a default animation when switching visual state from any state to any state. Visibility of the button cannot be animated because it is of a enumeration (discrete) type, but the opacity is a double number and this parameter is considered when we add a transition animation.

Expression Blend has yet one great feature here. In the upper right corner of the “States” tab you have a button with arrow and a little play symbol. This enables the visual states transitions preview mode. When you click it the transitions will be animated in the designer anytime you change the state selection.

Ok we have visual states defined, but how can we change the current state of our view. Here the behaviors and the triggers come in help. We can choose from existing behaviors and triggers or write the custom one. For this example need we will use the existing one. Go to “Assets” tab and in “Behaviors” section find DataStateBehavior. Drag it down and drop somewhere inside the window.

Now with the behavior selected go to “Properties” tab and define the binding as displayed below. This behavior simple will set the BlueState when the value of SwitchState is false and GreenState when it is true.

 

All those operations results in following XAML

 

 

If our application would have more that two states we would probably use triggers and GoToStateAction which in this case would look like this:

 

Cheers