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并将名为 CommonStates 的 添加到VisualStateGroupVisualStateManager.VisualStatesGroups附加属性。 该示例定义 MouseOver 中的 CommonStatesVisualStateGroupNormalVisualState 对象。 当用户将鼠标指针移到 上 Rectangle时,它会在半秒内从红色变为绿色。 当用户将鼠标从矩形移开时, Grid 会立即变回红色。 请注意, Normal 状态未定义 Storyboard。 不需要 ,Storyboard因为当 从 MouseOver 状态转换到 Normal 状态时RectangleStoryboard 将停止 , MouseOverColorSolidColorBrush 属性将返回红色。

<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还允许指定控件何时进入特定状态。 应调用以更改状态的方法取决于你的方案。 如果创建的控件 VisualStateManager 在其 ControlTemplate中使用 ,请调用 GoToState 方法。 有关如何创建使用 的 VisualStateManager控件的详细信息,请参阅 创建具有可自定义外观的控件。 例如,VisualStateManager如果使用 (的外部ControlTemplate,如果在 或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

获取 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)

适用于