Skip to content

Instantly share code, notes, and snippets.

@Vanlalhriata
Last active October 6, 2015 11:08
Show Gist options
  • Save Vanlalhriata/c1994d1260fce0cb9b6e to your computer and use it in GitHub Desktop.
Save Vanlalhriata/c1994d1260fce0cb9b6e to your computer and use it in GitHub Desktop.
Trigger XAML visual state changes from viewModels
<Window 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"
x:Name="window"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
x:Class="MvvmLightExtended.MainWindow"
Width="800"
Height="600"
>
<i:Interaction.Triggers>
<ei:DataTrigger Binding="{Binding IsChecked, ElementName=checkBox}" Value="True">
<ei:GoToStateAction StateName="VisualState1" TargetName="LayoutRoot"/>
</ei:DataTrigger>
<ei:DataTrigger Binding="{Binding IsChecked, ElementName=checkBox}" Value="False">
<ei:GoToStateAction StateName="VisualState2" TargetName="LayoutRoot"/>
</ei:DataTrigger>
</i:Interaction.Triggers>
<!-- Use this instead for multiple states known to the viewModel -->
<i:Interaction.Triggers>
<ei:PropertyChangedTrigger Binding="{Binding VisualStateChangeTrigger}">
<ei:GoToStateAction TargetObject="{Binding ElementName=_this}" StateName="{Binding TargetVisualState}"/>
</ei:PropertyChangedTrigger>
</i:Interaction.Triggers>
<!-- To change visual states:
VisualStateChangeTrigger = !VisualStateChangeTrigger;
-->
</Window>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment