VisualStateManager.GoToState(FrameworkElement, String, Boolean) 方法

定義

使控制項在兩個狀態之間切換。 使用這個方法,即可轉換具有 ControlTemplate 之控制項的狀態。

public:
 static bool GoToState(System::Windows::FrameworkElement ^ control, System::String ^ stateName, bool useTransitions);
public static bool GoToState (System.Windows.FrameworkElement control, string stateName, bool useTransitions);
static member GoToState : System.Windows.FrameworkElement * string * bool -> bool
Public Shared Function GoToState (control As FrameworkElement, stateName As String, useTransitions As Boolean) As Boolean

參數

control
FrameworkElement

控制項要在之間轉換的兩個狀態。

stateName
String

要切換的目標狀態。

useTransitions
Boolean

true 表示使用 VisualTransition 物件在狀態之間轉換,否則為 false

傳回

如果控制項成功轉換到新的狀態,則為 true,否則為 false

例外狀況

controlnull

-或-

stateNamenull

範例

下列範例示範使用 方法在狀態之間轉換的 GoToState 控制項。 如需整個範例,請參閱 建立具有可自訂外觀的控制項

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);
    }
}
Private Sub UpdateStates(ByVal useTransitions As Boolean)

    If Value >= 0 Then
        VisualStateManager.GoToState(Me, "Positive", useTransitions)
    Else
        VisualStateManager.GoToState(Me, "Negative", useTransitions)
    End If

    If IsFocused Then
        VisualStateManager.GoToState(Me, "Focused", useTransitions)
    Else
        VisualStateManager.GoToState(Me, "Unfocused", useTransitions)

    End If
End Sub

備註

GoToState如果您要變更控制項中在其 中使用 VisualStateManager 中的 ControlTemplate 狀態,請呼叫 方法。 GoToElementState例如,如果您在 VisualStateManagerUserControl 或 單一元素中使用) ,則呼叫 方法來變更 (外部 ControlTemplate 元素的狀態。

適用於