VisualStateManager.GoToState(FrameworkElement, String, Boolean) Method

Definition

Transitions the control between two states. Use this method to transition states on control that has a ControlTemplate.

public static bool GoToState(System.Windows.FrameworkElement control, string stateName, bool useTransitions);

Parameters

control
FrameworkElement

The control to transition between states.

stateName
String

The state to transition to.

useTransitions
Boolean

true to use a VisualTransition object to transition between states; otherwise, false.

Returns

true if the control successfully transitioned to the new state; otherwise, false.

Exceptions

control is null.

-or-

stateName is null.

Examples

The following example demonstrates a control that uses the GoToState method to transition between states. For the entire example, see Creating a Control That Has a Customizable Appearance.

private void UpdateStates(bool useTransitions)
{
    if (Value >= 0)
    {
        VisualStateManager.GoToState(this, "Positive", useTransitions);
    }
    else
    {
        VisualStateManager.GoToState(this, "Negative", useTransitions);
    }

    if (IsFocused)
    {
        VisualStateManager.GoToState(this, "Focused", useTransitions);
    }
    else
    {
        VisualStateManager.GoToState(this, "Unfocused", useTransitions);
    }
}

Remarks

Call the GoToState method if you are changing states in a control that uses the VisualStateManager in its ControlTemplate. Call the GoToElementState method to change states on an element outside of a ControlTemplate (for example, if you use a VisualStateManager in a UserControl or in a single element).

Applies to

Product Versions
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10