共用方式為


VisualStateManager 類別

定義

管理狀態和在控件狀態之間轉換的邏輯。

public ref class VisualStateManager : System::Windows::DependencyObject
public class VisualStateManager : System.Windows.DependencyObject
type VisualStateManager = class
    inherit DependencyObject
Public Class VisualStateManager
Inherits DependencyObject
繼承

範例

下列範例會建立 Rectangle,並將名為 CommonStatesVisualStateGroup 新增至 VisualStateManager.VisualStatesGroups 附加屬性。 此範例會定義 CommonStatesVisualStateGroup中的 MouseOverNormalVisualState 物件。 當使用者將滑鼠指標移至 Rectangle上方時,它會在半秒內從紅色變更為綠色。 當使用者將滑鼠從矩形移開時,Grid 會立即變更回紅色。 請注意,Normal 狀態不會定義 Storyboard。 不需要 Storyboard,因為當 RectangleMouseOver 狀態轉換為 Normal 狀態時,MouseOverStoryboard 會停止,而 SolidColorBrushColor 屬性會回到紅色。

<Rectangle Name="rect" 
           Width="100" Height="100"
           MouseEnter="rect_MouseEvent" 
           MouseLeave="rect_MouseEvent">
  <VisualStateManager.VisualStateGroups>
    <VisualStateGroup Name="MouseStates">
      <VisualState Name="MouseEnter">
        <Storyboard>
          <ColorAnimation To="Green" 
                          Storyboard.TargetName="rectBrush" 
                          Storyboard.TargetProperty="Color"/>
        </Storyboard>
      </VisualState>
      <VisualState Name="MouseLeave" />
      <VisualStateGroup.Transitions>
        <VisualTransition To="MouseLeave" GeneratedDuration="00:00:00"/>

        <VisualTransition To="MouseEnter" GeneratedDuration="00:00:00.5">
          <VisualTransition.GeneratedEasingFunction>
            <ExponentialEase EasingMode="EaseOut" Exponent="10"/>
          </VisualTransition.GeneratedEasingFunction>
        </VisualTransition>

      </VisualStateGroup.Transitions>
    </VisualStateGroup>
  </VisualStateManager.VisualStateGroups>

  <Rectangle.Fill>
    <SolidColorBrush x:Name="rectBrush" Color="Red"/>
  </Rectangle.Fill>
</Rectangle>

下列範例顯示上一個範例中定義的事件處理程式,並呼叫 GoToElementState 方法來轉換狀態。 如果上一個範例中的矩形是 ControlTemplate的一部分,則此範例必須呼叫 GoToState 方法。

private void rect_MouseEvent(object sender, MouseEventArgs e)
{
    if (rect.IsMouseOver)
    {
        VisualStateManager.GoToElementState(rect, "MouseEnter", true);
    }
    else
    {
        VisualStateManager.GoToElementState(rect, "MouseLeave", true);
    }
}
Private Sub rect_MouseEvent(ByVal sender As Object, ByVal e As MouseEventArgs)
    If rect.IsMouseOver Then
        VisualStateManager.GoToElementState(rect, "MouseEnter", True)
    Else
        VisualStateManager.GoToElementState(rect, "MouseLeave", True)
    End If
End Sub

備註

VisualStateManager 可讓您指定控件的狀態、控制項在特定狀態時的外觀,以及控件變更狀態時。 例如,當按下 Button 時,其外觀可能會與未按下時稍有不同。 兩個狀態,即 Button 定義對應至按下時("Pressed"),以及當它不是("Normal")。 當控件處於 狀態時的外觀是由 VisualState所定義。 VisualState 包含 Storyboard 物件的集合,指定控件處於該狀態時控件的外觀變更方式。 您可以藉由在控件上設定 VisualStateManager.VisualStateGroups 附加屬性,將視覺狀態新增至控件。 每個 VisualStateGroup 都包含互斥 VisualState 物件的集合。 也就是說,控件在每個 VisualStateGroup中一律處於 的一個狀態。

VisualStateManager 也可讓您指定控件何時進入特定狀態。 您應該呼叫 來變更狀態的方法取決於您的案例。 如果您建立在其 ControlTemplate中使用 VisualStateManager 的控件,請呼叫 GoToState 方法。 如需有關如何建立使用 VisualStateManager之控件的詳細資訊,請參閱 建立具有可自定義外觀的控件。 如果您在 ControlTemplate 外部使用 VisualStateManager(例如,如果您在 UserControl 或單一元素中使用 VisualStateManager),請呼叫 GoToElementState 方法。 在任一情況下,VisualStateManager 會執行適當啟動和停止與相關狀態相關聯分鏡腳本所需的邏輯。 例如,假設控件定義狀態,State1State2,每個狀態都有與其相關聯的分鏡腳本。 如果控件位於 State1 中,而且您傳遞 State2GoToStateGoToElementState,則 VisualStateManager 會在 State2 中啟動分鏡腳本,並在 State1中停止分鏡腳本。

Windows Presentation Foundation (WPF) 隨附的控件會使用 VisualStateManager 來變更視覺狀態。 當您為 WPF 隨附的控制項建立 ControlTemplate 時,您可以將 VisualState 物件新增至控件的 ControlTemplate,以指定控件在特定狀態的外觀。 若要尋找 WPF 隨附之控制元件的視覺狀態名稱,請參閱 控制項樣式和範本。 控件的邏輯會處理狀態之間的轉換,因此您不需要在新的 ControlTemplate中定義 VisualState 物件以外的任何動作。 如需如何建立現有控件控件範本的詳細資訊,請參閱 透過建立 ControlTemplate自定義現有控件的外觀。

如果您想要實作自己的邏輯以在狀態之間轉換,您必須繼承自 VisualStateManager、覆寫 GoToStateCore 方法,並在使用自定義邏輯的控件上設定 VisualStateManager.CustomVisualStateManager 附加屬性。

建構函式

VisualStateManager()

初始化 VisualStateManager 類別的新實例。

欄位

CustomVisualStateManagerProperty

識別 CustomVisualStateManager 相依性屬性。

VisualStateGroupsProperty

識別 VisualStateGroups 相依性屬性。

屬性

DependencyObjectType

取得包裝這個實例 CLR 類型的 DependencyObjectType

(繼承來源 DependencyObject)
Dispatcher

取得與這個 DispatcherObject 相關聯的 Dispatcher

(繼承來源 DispatcherObject)
IsSealed

取得值,這個值表示這個實例目前是否為密封狀態(只讀)。

(繼承來源 DependencyObject)

附加屬性

CustomVisualStateManager

取得或設定在控件狀態之間轉換的 VisualStateManager 物件。

VisualStateGroups

取得或設定 VisualStateGroup 物件的集合。

方法

CheckAccess()

判斷呼叫端線程是否可存取此 DispatcherObject

(繼承來源 DispatcherObject)
ClearValue(DependencyProperty)

清除屬性的本機值。 要清除的屬性是由 DependencyProperty 識別碼所指定。

(繼承來源 DependencyObject)
ClearValue(DependencyPropertyKey)

清除唯讀屬性的本機值。 要清除的屬性是由 DependencyPropertyKey指定。

(繼承來源 DependencyObject)
CoerceValue(DependencyProperty)

強制指定相依性屬性的值。 這可藉由叫用在相依性屬性的屬性元數據中指定的任何 CoerceValueCallback 函式來完成,因為它存在於呼叫 DependencyObject上。

(繼承來源 DependencyObject)
Equals(Object)

判斷提供的 DependencyObject 是否相當於目前的 DependencyObject

(繼承來源 DependencyObject)
GetCustomVisualStateManager(FrameworkElement)

取得附加 CustomVisualStateManager 屬性。

GetHashCode()

取得這個 DependencyObject的哈希碼。

(繼承來源 DependencyObject)
GetLocalValueEnumerator()

建立特製化列舉值,以判斷哪些相依性屬性在此 DependencyObject上設定值。

(繼承來源 DependencyObject)
GetType()

取得目前實例的 Type

(繼承來源 Object)
GetValue(DependencyProperty)

傳回 DependencyObject這個實例上相依性屬性的目前有效值。

(繼承來源 DependencyObject)
GetVisualStateGroups(FrameworkElement)

取得附加 VisualStateGroups 屬性。

GoToElementState(FrameworkElement, String, Boolean)

轉換兩種狀態之間的專案。 使用這個方法轉換應用程式所定義的狀態,而不是由控件定義。

GoToState(FrameworkElement, String, Boolean)

轉換兩種狀態之間的控制件。 使用這個方法,轉換具有 ControlTemplate之控件的狀態。

GoToStateCore(FrameworkElement, FrameworkElement, String, VisualStateGroup, VisualState, Boolean)

在狀態之間轉換控制件。

InvalidateProperty(DependencyProperty)

重新評估指定相依性屬性的有效值。

(繼承來源 DependencyObject)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
OnPropertyChanged(DependencyPropertyChangedEventArgs)

每當更新此 DependencyObject 上任何相依性屬性的有效值時叫用。 變更的特定相依性屬性會在事件數據中報告。

(繼承來源 DependencyObject)
RaiseCurrentStateChanged(VisualStateGroup, VisualState, VisualState, FrameworkElement, FrameworkElement)

在指定的 VisualStateGroup 對象上引發 CurrentStateChanging 事件。

RaiseCurrentStateChanging(VisualStateGroup, VisualState, VisualState, FrameworkElement, FrameworkElement)

在指定的 VisualStateGroup 對象上引發 CurrentStateChanging 事件。

ReadLocalValue(DependencyProperty)

如果相依性屬性存在,則傳回本機值。

(繼承來源 DependencyObject)
SetCurrentValue(DependencyProperty, Object)

設定相依性屬性的值,而不變更其值來源。

(繼承來源 DependencyObject)
SetCustomVisualStateManager(FrameworkElement, VisualStateManager)

設定附加 CustomVisualStateManager 屬性。

SetValue(DependencyProperty, Object)

設定相依性屬性的本機值,其相依性屬性標識符所指定。

(繼承來源 DependencyObject)
SetValue(DependencyPropertyKey, Object)

設定只讀相依性屬性的本機值,由相依性屬性 DependencyPropertyKey 標識碼所指定。

(繼承來源 DependencyObject)
ShouldSerializeProperty(DependencyProperty)

傳回值,這個值表示串行化進程是否應該串行化所提供相依性屬性的值。

(繼承來源 DependencyObject)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
VerifyAccess()

強制呼叫線程可以存取此 DispatcherObject

(繼承來源 DispatcherObject)

適用於