How to go back to "Base" state using VisualStateManager?
Asked Answered
C

3

16

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

Catamaran answered 2/10, 2009 at 8:29 Comment(1)
Good question. I wonder if Base is only used as "baseline" to determine if any properties have changed during design time editing of the visual states?Vegetarianism
B
5

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?

Blackfellow answered 2/10, 2009 at 8:43 Comment(2)
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?Catamaran
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.Blackfellow
V
21

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.

Vegetarianism answered 14/10, 2009 at 13:36 Comment(0)
A
16

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 );
Allethrin answered 16/10, 2009 at 16:6 Comment(4)
Are you sure the empty state will undo previous effects of the states?Catamaran
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.Allethrin
Where should i put the switch to the "BaseState"? Constructor?Fortis
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.Allethrin
B
5

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?

Blackfellow answered 2/10, 2009 at 8:43 Comment(2)
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?Catamaran
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.Blackfellow

© 2022 - 2024 — McMap. All rights reserved.