I know we can use
VisualStateManager.GoToState(this,"SomeState1",true);
to enter into SomeState1 , but now how to go back to the base state, like no state, the state where the control was loaded in.
VisualStateManager.GoToState(this,"base",true);
// OR
VisualStateManager.GoToState(this,"",true);
// OR
VisualStateManager.GoToState(this,null,true);
The problem is if there is no such way to go back to the initial or base state then I will have to always create a first state and in the contructor goto the first state in the start of control.
I didnt find any documentation, so I am trying all combinations but didnt find any working one..
Normal != Base.
Base is just the control's initial state before any visual state is applied (i.e. before the VSM is active).
If you read this article on the Expression blog there is a good description which I have lifted here:
... when you author your own templated control or UserControl, you should define a ‘default’ state in each state group. Have the control go to those ‘default’ states when it initializes, and do so with transitions suppressed so that it happens without delay. Once it’s on the state graph, the control is ready for state transitions to occur so now you can implement the event-handlers that trigger the transitions within the state graph.
From a brief look at the VSM source code, it appears there is no way to get out of the VSM and back to your original Base state... so yes, you do need a "Normal" state. :(
I also find this a bit annoying that the VSM state cannot be removed easily, although the above solution does makes sense. Maybe they will fix this in the future.
To do this, you have to first define your "base" state.
The deal is, if you define a visual state that doesn't contain a storyboard, then this state will effectively be equal to the "base" state - the one that the control was loaded in.
<VisualStateGroup x:Name="TheGroup">
<VisualState x:Name="SomeState1">
<Storyboard>
...
</Storyboard>
</VisualState>
<VisualState x:Name="BaseState" /> <!-- Note: the VisualState tag is empty -->
</VisualStateGroup>
Then switch to that state:
VisualStateManager.GoToState( this, "BaseState", true );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With