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

别说谁变了你拦得住时间么 提交于 2019-12-01 02:29:20

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?

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