问题
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