This is a mirror of official site: http://jasper-net.blogspot.com/

Trigger a Storyboard on ViewModel changes

| Wednesday, December 22, 2010
Interactions based on ViewModel changes are easy as soon as you understand how it works. A lot of people have been hiding and showing elements in the UI based on a boolean in the ViewModel which is converted to fit the Visibility property. Of course they used an IValueConverter that translates a bool to a Visibility enum.
But sometimes designers are tough, they don’t want to show and hide, they want to play a full animation. Of course I know about the ability to trigger a Storyboard by using EventTriggers (control Loaded or Clicked for example). But did you know about the DataTrigger?

DataTrigger

The DataTrigger comes Expression Blend, you’ll have to reference the Microsoft.Expression.Interactions library, which can be found here: C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\Silverlight\v4.0\Libraries\Microsoft.Expression.Interactions.dll
It’s not really that complex to use. If you only want to start a Storyboard when a specific value is put in the bound property the binding looks very much like this.

<Grid x:Name="LoginGrid">
 <i:Interaction.Triggers>
   <ei:DataTrigger Binding="{Binding IsLoggedIn}" Value="true">
     <ei:ControlStoryboardAction x:Name="FadeOutOnLogin" Storyboard="{StaticResource LoginFadeOut}"/>
   </ei:DataTrigger>
 </i:Interaction.Triggers>
</Grid>

Operators

If you have something more specific where you want to use an operator other than equal it will be just a little bit different. The below only starts the storyboard when the value is less than 1.

<Grid x:Name="LoginGrid">
 <i:Interaction.Triggers>
   <ei:DataTrigger Binding="{Binding IsLoggedIn}" Value="1" Comparison="LessThan">
     <ei:ControlStoryboardAction x:Name="FadeOutOnLogin" Storyboard="{StaticResource LoginFadeOut}"/>
   </ei:DataTrigger>
 </i:Interaction.Triggers>
</Grid>

You can use any of the following operators: Equal, NotEqual, LessThan, LessThanOrEqual, GreaterThan, GreaterThanOrEqual.

Really complex scenario’s

Alright, it will start to become more complex when you have more than just on condition that needs to be applied. It would be more clear to make use of a PropertyChangedTrigger in that case, and additional set Conditions. The below example gets a trigger if the IsLoggedIn is changed, and than will apply the conditions, greater than 1 and less than 10 in this example. If the outcome of the conditions is true the Storyboard will start.

<Grid x:Name="LoginGrid">
 <i:Interaction.Triggers>
   <ei:PropertyChangedTrigger Binding="{Binding IsLoggedIn}">
     <i:Interaction.Behaviors>
       <ei:ConditionBehavior>
         <ei:ConditionalExpression>
           <ei:ComparisonCondition LeftOperand="{Binding IsLoggedIn}" Operator="GreaterThan" RightOperand="1"/>
           <ei:ComparisonCondition LeftOperand="{Binding IsLoggedIn}" Operator="LessThan" RightOperand="10"/>
         </ei:ConditionalExpression

Read more: Mark Monster

Posted via email from .NET Info

0 comments: