VisualStateGroup 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
包含互斥的 VisualState 对象和用于从一种状态转移到另一种状态的 VisualTransition 对象。
public ref class VisualStateGroup : System::Windows::DependencyObject
[System.Windows.Markup.ContentProperty("States")]
[System.Windows.Markup.RuntimeNameProperty("Name")]
public class VisualStateGroup : System.Windows.DependencyObject
[<System.Windows.Markup.ContentProperty("States")>]
[<System.Windows.Markup.RuntimeNameProperty("Name")>]
type VisualStateGroup = class
inherit DependencyObject
Public Class VisualStateGroup
Inherits DependencyObject
- 继承
- 属性
示例
以下示例为包含一Grid个Button实例的简单ControlTemplate方法创建一个。 它还包含一个 VisualStateGroup 命名, CommonStates
用于定义 MouseOver
和 Normal
状态。 VisualStateGroup此外,还有一个VisualTransition指定当用户将鼠标指针移到鼠标指针上方Button时,将鼠标指针从绿色更改为红色需要半秒Grid的时间。
<ControlTemplate TargetType="Button">
<Grid >
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<!--Take one half second to trasition to the MouseOver state.-->
<VisualTransition To="MouseOver"
GeneratedDuration="0:0:0.5"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal" />
<!--Change the SolidColorBrush, ButtonBrush, to red when the
mouse is over the button.-->
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="ButtonBrush"
Storyboard.TargetProperty="Color" To="Red" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid.Background>
<SolidColorBrush x:Name="ButtonBrush" Color="Green"/>
</Grid.Background>
</Grid>
</ControlTemplate>
注解
每个 VisualStateGroup 对象都包含一个 VisualState 对象集合。 一 VisualState 个对象集合,该集合 Storyboard 指定控件在控件处于特定状态时控件的外观更改方式。 例如, Button 当按下时,它的外观可能略有不同,而不是按下时。 定义 Button 对应于按下 ("Pressed"
) 时和未 () "Normal"
时的两个状态。
通过在控件上设置VisualStateManager.VisualStateGroups
附加属性来添加到VisualState控件。 将相互排斥的状态放在同一 VisualStateGroup个状态中。 例如,有 CheckBox 两个 VisualStateGroup 对象。 一个包含状态、Normal
、MouseOver``Pressed
和Disabled
。 另一个包含状态、Checked``UnChecked
状态和 Indeterminate
。 可以CheckBox同时处于状态MouseOver``UnChecked
,但它不能同时处于MouseOver
状态和Pressed
状态。
尽管可以将对象添加到 VisualState 任何元素,但它们是一种特别有用的方法,使其他人能够重新定义对象的 Control视觉行为。 如果创建自定义控件, ControlTemplate则可以通过在其类定义中添加一个 TemplateVisualStateAttribute 状态来指定控件可以处于哪个状态。 然后,为控件创建新 ControlTemplate 对象的任何人都可以将对象添加到 VisualState 模板。 这使 TemplateVisualStateAttribute 设计器工具(如 Visual Studio 和 Blend for Visual Studio)能够公开控件的状态。 具有相同 TemplateVisualStateAttribute.GroupName 的州属于同一 VisualStateGroup个。
有关如何使用对象的详细信息,请参阅通过创建 ControlTemplate 自定义现有控件的外观。ControlTemplateVisualStateGroup 有关如何创建使用该 VisualStateManager控件的详细信息,请参阅 创建具有可自定义外观的控件。
该 Transitions 属性包含 VisualTransition 当控件在定义 VisualStateGroup状态之间切换时应用的对象。
构造函数
VisualStateGroup() |
初始化 VisualStateGroup 类的新实例。 |
属性
CurrentState |
获取当前应用到该控件的 VisualState。 |
DependencyObjectType |
获取包装 DependencyObjectType 此实例的 CLR 类型。 (继承自 DependencyObject) |
Dispatcher |
获取与此 Dispatcher 关联的 DispatcherObject。 (继承自 DispatcherObject) |
IsSealed |
获取一个值,该值指示此实例当前是否为密封的(只读)。 (继承自 DependencyObject) |
Name |
获取或设置 VisualStateGroup 的名称。 |
States |
获取互斥 VisualState 对象的集合。 |
Transitions |
获取 VisualTransition 对象的集合。 |
方法
事件
CurrentStateChanged |
在控件转换为其他状态后发生。 |
CurrentStateChanging |
在控件开始向其他状态转换时发生。 |