Create custom VisualState in xaml and manually set it in CodeBehind

和自甴很熟 提交于 2019-12-06 01:50:02

问题


I have a TabItem style, which has VisualStates.

<VisualState x:Name="MouseOver"> 
<!-- Tab turns bronze when mouseover -->
</VisualState>

Now I want to have a custom visual state and manually set the state in codebehind instead of relying on the MouseOver event.

<VisualState x:Name="CustomVisualState">
<!-- this will be a storyboard to cause flashing -->
</VisualState> 

Then I need to set it in CodeBehind.

MyTabItem.VisualState = CustomVisualState.  //something like this

回答1:


Have you tried VisualStateManager.GoToState(Control,"stateName",UseTransition); Takes a Control, string with the custom state name and a bool flag for using transitions.
Addtional Example here




回答2:


Try this,

VisualStateManager.GoToElementState(Control, "StateName", true/false);

or

VisualStateManager.GoToState(Control, "StateName", true/false);



回答3:


The VisualStateManager also enables you to specify when a control enters a specific state. The method that you should call to change states depends on your scenario. If you create a control that uses the VisualStateManager in its ControlTemplate, call the GoToState method. For more information about how to create controls that use the VisualStateManager, see Creating a Control That Has a Customizable Appearance. If you use the VisualStateManager outside of a ControlTemplate (for example, if you use a VisualStateManager in a UserControl or in a single element), call the GoToElementState method. In either case, the VisualStateManager performs the logic that is required to appropriately start and stop the storyboards that are associated with the involved state. - VisualStateManager Class

That is how different between GoToElementState and GoToState.



来源:https://stackoverflow.com/questions/7839235/create-custom-visualstate-in-xaml-and-manually-set-it-in-codebehind

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