Oct
6
Written by:
New Age Solution
10/6/2009 2:02 PM
DataStateSwitchBehavior is very useful when implementing any kind of MVVM. Thing of DataSwitchBehavior as switch statement of states. Based on bound data appropriate state will be selected.
"DataStateSwitchBehavior" extremely useful and a must in order to succesfully implement MVVM in Silverlight. Imagine having a control with lights red, yellow and green. And based on bound data control is responsible for lighting up the proper light. This also allows seperation of work between graphic designer and developer in that designer will create multiple states for highlighting the lights (on and off) and developer hooking up those states to the view model controller.
- Go to http://expressionblend.codeplex.com/ and download binaries
- Add reference to Expression.Samples.Interactivity.dll and Microsoft SDKs\Expression\Blend 3\Interactivity\Libraries\Silverlight\System.Windows.Interactivity.dll
- Add namespace to user control as seen highlighted bold
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:si="clr-namespace:Expression.Samples.Interactivity;assembly=Expression.Samples.Interactivity"
OR
- From Blend Under Assets drag and drop DataSwitchBehavior to the control that requires state to be selected from multiple states based on binding data.

- Bind the data
<i:Interaction.Behaviors>
<si:DataStateSwitchBehavior Binding="{Binding CanLightRed, Mode=OneWay}">
<si:DataStateSwitchCase State="RedLayerOff" Value="false"/>
<si:DataStateSwitchCase State="RedLayerOn" Value="true"/>
si:DataStateSwitchBehavior>
i:Interaction.Behaviors>
That is all to use it.
Download the code sample with DataBehavior and basic MVVM implementation