5
votes

Kind of a quick question: Is it possible to activate a viewstate from XAML? I have been only able to activate one from CS, using the VisualStateManager.GotoState() method. This would fix some of my MVVM issues if it were easily possible.

Thanks

1

1 Answers

7
votes

If you are familiar with Blend behaviors, triggers, and actions there is a GoToStateAction which is a part of the Microsoft.Expression.Interactivity.Core namespace. You will have to reference the interactivity assemblies which are part of the Blend SDK.

Once you have the references set up it's as easy as specifying the GoToStateAction to react to some sort of trigger... all in XAML. Here is an example which fires the action off of the Loaded event using an EventTrigger:

<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions">
    <Grid x:Name="LayoutRoot">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <ic:GoToStateAction StateName="MyVisualState"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        ...
    </Grid>
</UserControl>

More info and tutorial about the specific GoToState action here.

EDIT: This answer is specific to Silverlight, not sure if this is available in WPF.