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
- 繼承
- 屬性
範例
下列範例會為 Button 包含一個 Grid 的 建立簡單 ControlTemplate 。 它也包含名為 CommonStates
的 VisualStateGroup ,其會 MouseOver
定義 和 Normal
狀態。 VisualStateGroup也有 , VisualTransition 指定當使用者將滑鼠指標移至 上方時,需要一半秒 Grid 的時間,才能將 變更為綠色。 Button
<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 。
如需如何在 中使用 VisualStateGroup ControlTemplate 物件的詳細資訊,請參閱 建立 ControlTemplate 來自訂現有控制項的外觀。 如需如何建立使用 之 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 |
在控制項開始轉換至不同狀態時發生。 |