How to go back to “Base” state using VisualStateManager?

China☆狼群 提交于 2019-12-03 23:41:27

问题


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


回答1:


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?




回答2:


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.




回答3:


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 );


来源:https://stackoverflow.com/questions/1508323/how-to-go-back-to-base-state-using-visualstatemanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!