views:

958

answers:

3

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..

+5  A: 

The default controls define a "Normal" visual state in the CommonStates group, which is reverted to on mouseout etc. I think you'll need to follow the same pattern for what I assume is a custom control?

Steven Robbins
So Normal is defined automatically by every control or for custom control i will have to define and use it as normal state as beginning state? Which one will be best place to switch to normal state ? in the constructor?
Akash Kava
You shouldn't have to switch to it, normal is just a state with no properties changed, you can then use that with GoToState to effectively "remove" any changes caused by another state.
Steven Robbins
+6  A: 

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.

Schneider
+5  A: 

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 );
Fyodor Soikin
Are you sure the empty state will undo previous effects of the states?
Akash Kava
Yes, I am. The storyboards inside VisualStates define differences from the "base" state. The VisualStateManager is smart enough to see which properties are changed and how. That's how it cam autogenerate transitions.In fact, why don't you just verify it by yourself? It's easy.Or otherwise, you can just take my word for it: I've been using this mechanism for quite some time now.
Fyodor Soikin
@Fyodor Soikin: Where should i put the switch to the "BaseState"? Constructor?
Jehof
Nowhere. A control is in "base" state unless is has been explicitly put into another state. Therefore, you don't have to put it into the "base" state explicitly.
Fyodor Soikin

related questions