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 建立 ,並將名為 CommonStatesVisualStateManager.VisualStatesGroups 加入 VisualStateGroup 至附加屬性。 此範例會 MouseOver 定義 中的 CommonStatesVisualStateGroupNormalVisualState 物件。 當使用者將滑鼠指標移至 上方 Rectangle 時,會將滑鼠指標從紅色變更為綠色,超過一半秒。 當使用者將滑鼠從矩形移開時,會 Grid 立即變更回紅色。 請注意, Normal 狀態不會定義 Storyboard 。 不需要 , Storyboard 因為 當 從 MouseOver 狀態轉換成 Normal 狀態時 RectangleStoryboardMouseOver 會停止 的 ,而 Color 的 屬性 SolidColorBrush 會傳回紅色。

<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也可讓您指定控制項何時進入特定狀態。 您應該呼叫 以變更狀態的方法取決於您的案例。 如果您建立在其 中使用 的 VisualStateManagerControlTemplate 控制項,請呼叫 GoToState 方法。 如需如何建立使用 之 VisualStateManager 控制項的詳細資訊,請參閱 建立具有可自訂外觀的控制項。 例如,如果您使用 VisualStateManager (外部 ControlTemplate ,如果您在 UserControlVisualStateManager 或 單一元素中使用) ,請呼叫 GoToElementState 方法。 不論是哪一種情況,都 VisualStateManager 執行適當啟動和停止與相關狀態相關聯的分鏡腳本所需的邏輯。 例如,假設控制項會定義狀態, State1 而且 State2 每個狀態都有與其相關聯的分鏡腳本。 如果控制項位於 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

DependencyObjectType取得包裝這個實例之 CLR 型別的 。

(繼承來源 DependencyObject)
Dispatcher

取得與這個 Dispatcher 關聯的 DispatcherObject

(繼承來源 DispatcherObject)
IsSealed

取得值,這個值表示此執行個體目前是否已密封 (唯讀)。

(繼承來源 DependencyObject)

附加屬性

CustomVisualStateManager

取得或設定 VisualStateManager 物件,可轉換控制項的狀態。

VisualStateGroups

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

方法

CheckAccess()

判斷呼叫的執行是否可以存取這個 DispatcherObject

(繼承來源 DispatcherObject)
ClearValue(DependencyProperty)

清除屬性的區域數值。 要清除的屬性是由 DependencyProperty 識別項所指定。

(繼承來源 DependencyObject)
ClearValue(DependencyPropertyKey)

清除唯讀屬性的區域數值。 要清除的屬性是由 DependencyPropertyKey 所指定。

(繼承來源 DependencyObject)
CoerceValue(DependencyProperty)

強制轉型所指定相依性屬性的值。 完成方式是叫用存在於呼叫 DependencyObject 之相依性屬性的屬性中繼資料中所指定的任何 CoerceValueCallback 函式。

(繼承來源 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)

對指定的 CurrentStateChanging 物件引發 VisualStateGroup 事件。

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

對指定的 CurrentStateChanging 物件引發 VisualStateGroup 事件。

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)

適用於