VisualStateManager 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
管理控件的状态以及用于状态过渡的逻辑。
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
中的 CommonStates
VisualStateGroup和 Normal
VisualState 对象。 当用户将鼠标指针移到 上 Rectangle时,它会在半秒内从红色变为绿色。 当用户将鼠标从矩形移开时, Grid 会立即变回红色。 请注意, Normal
状态未定义 Storyboard。 不需要 ,Storyboard因为当 从 MouseOver
状态转换到 Normal
状态时Rectangle, Storyboard 将停止 , MouseOver
的 ColorSolidColorBrush 属性将返回红色。
<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 都执行适当启动和停止与所涉及的状态关联的情节提要所需的逻辑。 例如,假设控件定义了状态 State1
和 State2
,其中每个状态都有一个与之关联的情节提要。 如果 控件在 中 State1
,并且你传递给 State2
GoToState 或 GoToElementState,则会 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 对象的集合。 |