通过


VisualStateManager 类

定义

管理状态和在控件状态之间转换的逻辑。

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

示例

以下示例创建一个RectangleVisualStateGroup命名CommonStates属性并将其添加到VisualStateManager.VisualStatesGroups附加属性。 该示例定义 MouseOver . 中的CommonStatesVisualStateGroup对象和NormalVisualState对象。 当用户将鼠标指针移到鼠标指针上方 Rectangle时,它会在半秒内从红色变为绿色。 当用户将鼠标从矩形移开时, Grid 立即变为红色。 请注意,状态 Normal 未定义 Storyboard。 不需要 AStoryboard,因为当从MouseOver状态转换到Normal状态时RectangleStoryboard将停止 forMouseOverColor并且返回的属性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 当按下时,它的外观可能与未按下时略有不同。 定义对应于按下时("Pressed")和未"Normal"按下时的两种状态Button。 控件处于状态时的外观由 a VisualState. 一 VisualState 个对象集合 Storyboard ,这些对象指定控件处于该状态时控件的外观更改方式。 通过在控件上设置 VisualStateManager.VisualStateGroups 附加属性,向控件添加视觉状态。 每个 VisualStateGroup 对象都包含互斥的对象集合 VisualState 。 也就是说,控件始终处于每个 VisualStateGroup状态的一个状态。

此外 VisualStateManager ,还可以指定控件何时进入特定状态。 应调用更改状态的方法取决于你的方案。 如果创建使用该 VisualStateManager 控件的 ControlTemplate控件,请调用该方法 GoToState 。 有关如何创建使用该 VisualStateManager控件的控件的详细信息,请参阅 创建具有可自定义外观的控件。 如果使用 VisualStateManager 外部( ControlTemplate 例如,如果在 VisualStateManager 一个 UserControl 元素中使用或单个元素中),请调用该方法 GoToElementState 。 在任一情况下,执行 VisualStateManager 适当启动和停止与相关状态关联的情节提要所需的逻辑。 例如,假设控件定义了状态, State1 每个 State2控件都有一个与之关联的情节提要。 如果控件处于传入State1状态,或者传入或GoToElementStateVisualStateManager传入State2GoToState,则启动情节提State2要并停止情节提要。State1

Windows Presentation Foundation(WPF)附带的控件用于 VisualStateManager 更改视觉状态。 为 WPF 附带的控件创建对象ControlTemplate时,可以向控件ControlTemplate添加VisualState对象以指定控件在某个状态下的外观。 若要查找 WPF 附带的控件的视觉状态的名称,请参阅 控件样式和模板。 控件的逻辑处理状态之间的转换,因此无需执行除定义 VisualStateControlTemplate对象以外的任何操作。 有关如何为现有控件创建控件模板的详细信息,请参阅 如何为控件创建模板

如果要实现自己的逻辑以在状态之间转换,则必须继承 VisualStateManager、重写 GoToStateCore 该方法,并在使用自定义逻辑的控件上设置 VisualStateManager.CustomVisualStateManager 附加属性。

构造函数

名称 说明
VisualStateManager()

初始化 VisualStateManager 类的新实例。

字段

名称 说明
CustomVisualStateManagerProperty

标识 CustomVisualStateManager 依赖属性。

VisualStateGroupsProperty

标识 VisualStateGroups 依赖属性。

属性

名称 说明
DependencyObjectType

获取包装 DependencyObjectType 此实例的 CLR 类型。

(继承自 DependencyObject)
Dispatcher

获取与此DispatcherDispatcherObject关联的值。

(继承自 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)

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)

适用于