VisualStateManager VisualStateManager VisualStateManager VisualStateManager Class

定義

管理控制項在狀態之間轉換的狀態與邏輯。Manages states and the logic for transitioning between states for controls.

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

範例

下列Rectangle範例會建立, 並VisualStateGroup將名CommonStates為的VisualStateManager.VisualStatesGroups加入至附加屬性。The following example creates a Rectangle and adds a VisualStateGroup named CommonStates to the VisualStateManager.VisualStatesGroups attached property. 此範例會在MouseOverNormal VisualState CommonStates定義VisualStateGroup和物件。The example defines the MouseOver and NormalVisualState objects in the CommonStatesVisualStateGroup. 當使用者將滑鼠指標移至上方Rectangle時, 它會在半秒內從紅色變更為綠色。When the user moves the mouse pointer over the Rectangle, it changes from red to green over one half second. 當使用者將滑鼠移開矩形時, Grid會立即將變更回紅色。When the user moves the mouse away from the rectangle, the Grid immediately changes back to red. 請注意, Normal狀態不會Storyboard定義。Note that the Normal state does not define a Storyboard. Rectangle MouseOver Storyboard MouseOver 不需要SolidColorBrush , 因為當Color從狀態轉換到狀態時,的會停止,且的屬性會傳回Normal Storyboard到紅色。A Storyboard is not required because when the Rectangle transitions from the MouseOver state to the Normal state, the Storyboard for MouseOver is stopped and the Color property for the SolidColorBrush returns to red.

<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以在狀態之間轉換。The following example shows the event handler that is defined in the previous example and calls the GoToElementState method to transition between states. 如果上一個範例中的矩形是的一部分ControlTemplate, 則此範例必須GoToState呼叫方法。If the rectangle in the previous example was part of a ControlTemplate, the example would have to call the GoToState method.

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讓您指定控制項的狀態、控制項處於特定狀態時的外觀, 以及控制項變更狀態的時機。The VisualStateManager enables you to specify states for a control, the appearance of a control when it is in a certain state, and when a control changes states. 例如, 當按Button下 [未按下] 時, 可能會有稍微不同的外觀。For example, a Button might have a slightly different appearance when it is pressed than when it is not pressed. 當按下時Button , 定義的兩個狀態 ("Pressed"), 如果不是 ("Normal"), 則會對應至。Two states that the Button defines correspond to when it is pressed ("Pressed") and when it is not ("Normal"). 控制項處於狀態時的外觀是由VisualState所定義。The appearance of a control when it is in a state is defined by a VisualState. VisualState包含Storyboard物件的集合, 這些物件會指定控制項的外觀在控制項處於該狀態時的變更方式。A VisualState contains a collection of Storyboard objects that specify how the control's appearance changes when the control is in that state. 您可以藉由設定控制項上的VisualStateManager.VisualStateGroups附加屬性, 將視覺狀態加入至控制項。You add visual states to a control by setting the VisualStateManager.VisualStateGroups attached property on the control. VisualStateGroup個都包含互斥VisualState的物件集合。Each VisualStateGroup contains a collection of VisualState objects that are mutually exclusive. 也就是說, 控制項在每個VisualStateGroup中一律只會處於一個的狀態。That is, the control is always in exactly one state of in each VisualStateGroup.

VisualStateManager也可讓您指定控制項進入特定狀態的時間。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. 如果您建立的控制項VisualStateManager在其ControlTemplate中使用, 請呼叫GoToState方法。If you create a control that uses the VisualStateManager in its ControlTemplate, call the GoToState method. 如需如何建立使用VisualStateManager之控制項的詳細資訊, 請參閱建立具有可自訂外觀的控制項For more information about how to create controls that use the VisualStateManager, see Creating a Control That Has a Customizable Appearance. 如果VisualStateManager您使用外部VisualStateManager ControlTemplate的 (例如, 如果UserControl您在單一專案的或中使用), 請呼叫GoToElementState方法。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. 不論是哪一種VisualStateManager情況, 都會執行必要的邏輯, 以適當地啟動和停止與所涉及狀態相關聯的分鏡腳本。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. 例如, 假設某個控制項定義了狀態, State1而且State2每個都有一個與其相關聯的分鏡腳本。For example, suppose that a control defines the states, State1 and State2, each of which has a storyboard associated with it. 如果控制項在中State1 , 而且您將傳遞GoToState State2GoToElementState或, VisualStateManager則會在中State2啟動腳本, 並停止State1中的分鏡腳本。If the control is in State1 and you pass State2 to GoToState or GoToElementState, the VisualStateManager starts the storyboard in State2 and stops the storyboard in State1.

隨附Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF)的控制項會VisualStateManager使用來變更視覺狀態。Controls that are included with Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) use the VisualStateManager to change visual states. 當您ControlTemplate針對WPFWPF隨附的控制項建立時, 您可以將物件加入VisualState至控制項的ControlTemplate , 以指定控制項在特定狀態中的外觀。When you create a ControlTemplate for a control that is included with WPFWPF, you can add VisualState objects to the control's ControlTemplate to specify the control's appearance in a certain state. 若要尋找隨附WPFWPF之控制項的視覺狀態名稱, 請參閱控制項樣式和範本To find the names of the visual states for the controls that are included with WPFWPF, see Control Styles and Templates. 控制項的邏輯會處理狀態間的轉換, 因此除了在新VisualState ControlTemplate的中定義物件之外, 您不需要執行任何動作。The control's logic handles transitioning between states, so you do not need to do anything other than define the VisualState objects in the new ControlTemplate. 如需如何為現有控制項建立控制項範本的詳細資訊, 請參閱藉由建立 ControlTemplate 自訂現有控制項的外觀For more information about how to create control templates for existing controls, see Customizing the Appearance of an Existing Control by Creating a ControlTemplate.

如果您想要實作為在狀態之間轉換的邏輯, 您必須繼承VisualStateManager自、覆GoToStateCoreVisualStateManager.CustomVisualStateManager方法, 並在使用自訂邏輯的控制項上設定附加屬性。If you want to implement your own logic for transitioning between states, you must inherit from VisualStateManager, override the GoToStateCore method, and set the VisualStateManager.CustomVisualStateManager attached property on the control that uses the custom logic.

建構函式

VisualStateManager() VisualStateManager() VisualStateManager() VisualStateManager()

初始化 VisualStateManager 類別的新執行個體。Initializes a new instance of the VisualStateManager class.

欄位

CustomVisualStateManagerProperty CustomVisualStateManagerProperty CustomVisualStateManagerProperty CustomVisualStateManagerProperty

識別 CustomVisualStateManager 相依性屬性。Identifies the CustomVisualStateManager dependency property.

VisualStateGroupsProperty VisualStateGroupsProperty VisualStateGroupsProperty VisualStateGroupsProperty

識別 VisualStateGroups 相依性屬性。Identifies the VisualStateGroups dependency property.

附加屬性

CustomVisualStateManager CustomVisualStateManager CustomVisualStateManager CustomVisualStateManager

取得或設定 VisualStateManager 物件,可轉換控制項的狀態。Gets or sets the VisualStateManager object that transitions between the states of a control.

VisualStateGroups VisualStateGroups VisualStateGroups VisualStateGroups

取得或設定 VisualStateGroup 物件的集合。Gets or sets a collection of VisualStateGroup objects.

方法

CheckAccess() CheckAccess() CheckAccess() CheckAccess()

判斷呼叫的執行是否可以存取這個 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)
ClearValue(DependencyProperty) ClearValue(DependencyProperty) ClearValue(DependencyProperty) ClearValue(DependencyProperty)

清除屬性的區域數值。Clears the local value of a property. 要清除的屬性是由 DependencyProperty 識別項所指定。The property to be cleared is specified by a DependencyProperty identifier.

(Inherited from DependencyObject)
ClearValue(DependencyPropertyKey) ClearValue(DependencyPropertyKey) ClearValue(DependencyPropertyKey) ClearValue(DependencyPropertyKey)

清除唯讀屬性的區域數值。Clears the local value of a read-only property. 要清除的屬性是由 DependencyPropertyKey 所指定。The property to be cleared is specified by a DependencyPropertyKey.

(Inherited from DependencyObject)
CoerceValue(DependencyProperty) CoerceValue(DependencyProperty) CoerceValue(DependencyProperty) CoerceValue(DependencyProperty)

強制轉型所指定相依性屬性的值。Coerces the value of the specified dependency property. 叫用存在於呼叫 CoerceValueCallback 上相依性屬性之屬性中繼資料內所指定的任何 DependencyObject 函式,就可以達到這一點。This is accomplished by invoking any CoerceValueCallback function specified in property metadata for the dependency property as it exists on the calling DependencyObject.

(Inherited from DependencyObject)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷提供的 DependencyObject 和目前的 DependencyObject 是否相等。Determines whether a provided DependencyObject is equivalent to the current DependencyObject.

(Inherited from DependencyObject)
GetCustomVisualStateManager(FrameworkElement) GetCustomVisualStateManager(FrameworkElement) GetCustomVisualStateManager(FrameworkElement) GetCustomVisualStateManager(FrameworkElement)

取得 CustomVisualStateManager 附加屬性。Gets the CustomVisualStateManager attached property.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

取得這個 DependencyObject 的雜湊程式碼。Gets a hash code for this DependencyObject.

(Inherited from DependencyObject)
GetLocalValueEnumerator() GetLocalValueEnumerator() GetLocalValueEnumerator() GetLocalValueEnumerator()

建立特定的列舉值,以判斷哪些相依性屬性在此 DependencyObject 上具有本機設定的值。Creates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject.

(Inherited from DependencyObject)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
GetValue(DependencyProperty) GetValue(DependencyProperty) GetValue(DependencyProperty) GetValue(DependencyProperty)

傳回 DependencyObject 的這個執行個體上之相依性屬性的目前有效值。Returns the current effective value of a dependency property on this instance of a DependencyObject.

(Inherited from DependencyObject)
GetVisualStateGroups(FrameworkElement) GetVisualStateGroups(FrameworkElement) GetVisualStateGroups(FrameworkElement) GetVisualStateGroups(FrameworkElement)

取得 VisualStateGroups 附加屬性。Gets the VisualStateGroups attached property.

GoToElementState(FrameworkElement, String, Boolean) GoToElementState(FrameworkElement, String, Boolean) GoToElementState(FrameworkElement, String, Boolean) GoToElementState(FrameworkElement, String, Boolean)

會讓項目在兩個狀態之間轉換。Transitions the element between two states. 使用此方法轉換應用程式所定義的狀態,而不是控制項所定義的狀態。Use this method to transition states that are defined by an application, rather than defined by a control.

GoToState(FrameworkElement, String, Boolean) GoToState(FrameworkElement, String, Boolean) GoToState(FrameworkElement, String, Boolean) GoToState(FrameworkElement, String, Boolean)

使控制項在兩個狀態之間切換。Transitions the control between two states. 使用這個方法,即可轉換具有 ControlTemplate 之控制項的狀態。Use this method to transition states on control that has a ControlTemplate.

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

會讓控制項在狀態之間轉換。Transitions a control between states.

InvalidateProperty(DependencyProperty) InvalidateProperty(DependencyProperty) InvalidateProperty(DependencyProperty) InvalidateProperty(DependencyProperty)

重新評估指定之相依性屬性的有效值。Re-evaluates the effective value for the specified dependency property

(Inherited from DependencyObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
OnPropertyChanged(DependencyPropertyChangedEventArgs) OnPropertyChanged(DependencyPropertyChangedEventArgs) OnPropertyChanged(DependencyPropertyChangedEventArgs) OnPropertyChanged(DependencyPropertyChangedEventArgs)

每當這個 DependencyObject 上任何相依性屬性的有效值已更新時叫用。Invoked whenever the effective value of any dependency property on this DependencyObject has been updated. 已變更的特定相依性屬性會在事件資料中報告。The specific dependency property that changed is reported in the event data.

(Inherited from DependencyObject)
RaiseCurrentStateChanged(VisualStateGroup, VisualState, VisualState, FrameworkElement, FrameworkElement) RaiseCurrentStateChanged(VisualStateGroup, VisualState, VisualState, FrameworkElement, FrameworkElement) RaiseCurrentStateChanged(VisualStateGroup, VisualState, VisualState, FrameworkElement, FrameworkElement) RaiseCurrentStateChanged(VisualStateGroup, VisualState, VisualState, FrameworkElement, FrameworkElement)

對指定的 CurrentStateChanging 物件引發 VisualStateGroup 事件。Raises the CurrentStateChanging event on the specified VisualStateGroup object.

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

對指定的 CurrentStateChanging 物件引發 VisualStateGroup 事件。Raises the CurrentStateChanging event on the specified VisualStateGroup object.

ReadLocalValue(DependencyProperty) ReadLocalValue(DependencyProperty) ReadLocalValue(DependencyProperty) ReadLocalValue(DependencyProperty)

傳回相依性屬性的區域值 (如果存在)。Returns the local value of a dependency property, if it exists.

(Inherited from DependencyObject)
SetCurrentValue(DependencyProperty, Object) SetCurrentValue(DependencyProperty, Object) SetCurrentValue(DependencyProperty, Object) SetCurrentValue(DependencyProperty, Object)

設定相依性屬性的值,而不需要變更其值來源。Sets the value of a dependency property without changing its value source.

(Inherited from DependencyObject)
SetCustomVisualStateManager(FrameworkElement, VisualStateManager) SetCustomVisualStateManager(FrameworkElement, VisualStateManager) SetCustomVisualStateManager(FrameworkElement, VisualStateManager) SetCustomVisualStateManager(FrameworkElement, VisualStateManager)

設定 CustomVisualStateManager 附加屬性。Sets the CustomVisualStateManager attached property.

SetValue(DependencyProperty, Object) SetValue(DependencyProperty, Object) SetValue(DependencyProperty, Object) SetValue(DependencyProperty, Object)

設定相依性屬性的區域值 (由相依性屬性的識別碼所指定)。Sets the local value of a dependency property, specified by its dependency property identifier.

(Inherited from DependencyObject)
SetValue(DependencyPropertyKey, Object) SetValue(DependencyPropertyKey, Object) SetValue(DependencyPropertyKey, Object) SetValue(DependencyPropertyKey, Object)

設定唯讀相依性屬性的區域數值 (由相依性屬性的 DependencyPropertyKey 識別項所指定)。Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property.

(Inherited from DependencyObject)
ShouldSerializeProperty(DependencyProperty) ShouldSerializeProperty(DependencyProperty) ShouldSerializeProperty(DependencyProperty) ShouldSerializeProperty(DependencyProperty)

傳回值,這個值表示序列化程序是否應該序列化所提供相依性屬性的值。Returns a value that indicates whether serialization processes should serialize the value for the provided dependency property.

(Inherited from DependencyObject)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)
VerifyAccess() VerifyAccess() VerifyAccess() VerifyAccess()

請強制執行可以存取這個 DispatcherObject 的呼叫執行緒。Enforces that the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)

屬性

DependencyObjectType DependencyObjectType DependencyObjectType DependencyObjectType

取得包裝此執行個體之 CLRCLR 型別的 DependencyObjectTypeGets the DependencyObjectType that wraps the CLRCLR type of this instance.

(Inherited from DependencyObject)
Dispatcher Dispatcher Dispatcher Dispatcher

取得與這個 Dispatcher 關聯的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(Inherited from DispatcherObject)
IsSealed IsSealed IsSealed IsSealed

取得值,這個值表示此執行個體目前是否已密封 (唯讀)。Gets a value that indicates whether this instance is currently sealed (read-only).

(Inherited from DependencyObject)

適用於