Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change VisualState via ViewModel [duplicate]

I know this Question is similar to many. Anyway, I don't understand.

I have a several VisualStates (more than 2, thats why DataStateBehavior is not my solution). And I have ViewModel, which have enum property CurrentState. Every enum value represents to one state, also may be several enum values represents to one states, doesn't metter. I want VisualState changed when the CurrentState changed (thought, that immediately appears in my mind : Binding was created exactly for this case!)

Can I bind CurrentState with view VisualState (xaml-only solution), to get the behavior described above?

If yes, how can I do it?

If no, how should I use VisualStateManager.GoToState() method in my ViewModel?

like image 676
stukselbax Avatar asked Dec 04 '22 07:12

stukselbax


1 Answers

I wanted to note a solution similar to @FasterSolutions', using built-in components of the Blend SDK.

Set a PropertyChangedTrigger on the view-model's "CurrentState" property, and add a GoToStateAction to change the visual state:

<i:Interaction.Triggers
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Inte‌​ractivity"  
    xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microso‌ft.Expression.Interactions">
    <ei:PropertyChangedTrigger Binding="{Binding CurrentState}">
        <ei:GoToStateAction StateName="{Binding CurrentState}" />
    </ei:PropertyChangedTrigger>
</i:Interaction.Triggers>
like image 181
McGarnagle Avatar answered Dec 17 '22 07:12

McGarnagle