Share via


VisualStateManager.VisualStateGroups 附加屬性

定義

取得或設定 VisualStateGroup 物件的集合。

see GetVisualStateGroups
see GetVisualStateGroups
see GetVisualStateGroups

範例

下列範例會為 包含一個 GridButton 建立簡單 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 都包含 物件的集合 VisualStateVisualState包含 物件的集合 Storyboard ,指定控制項在特定狀態時控制項的外觀變更方式。 例如, Button 當按下時,可能會有稍微不同的外觀,而不是按下時。 定義 Button 在按下 ("Pressed") 和未 () "Normal" 時對應的兩個狀態。

您可以在 控制項上設定 VisualStateGroups 附加屬性,以新增 VisualState 至控制項。 您會將彼此互斥的狀態放在相同的 VisualStateGroup 中。 例如,有 CheckBoxVisualStateGroup 個 物件。 其中一個包含狀態、 NormalMouseOver Pressed 、 和 Disabled 。 另一個包含狀態、 CheckedUnCheckedIndeterminateCheckBox可以同時處於 狀態 MouseOver UnChecked ,但不能同時處於 MouseOverPressed 狀態。

雖然您可以將物件新增 VisualState 至任何元素,但它們是一種特別有用的方式,可讓其他人重新定義 的 Control 視覺行為。 如果您建立使用 ControlTemplate 的自訂控制項,您可以藉由在其類別定義上加入 TemplateVisualStateAttribute ,來指定控制項可以位於哪個狀態。 然後,為控制項建立新 ControlTemplate 物件的任何人都可以將物件新增 VisualState 至範本。 相同 System.Windows.TemplateVisualStateAttribute.GroupName 狀態屬於相同的 VisualStateGroup

如需如何在 中使用 VisualStateGroup ControlTemplate 物件的詳細資訊,請參閱 建立 ControlTemplate 自訂現有控制項的外觀。 如需有關如何建立使用 VisualStateManager 之控制項的詳細資訊,請參閱 建立具有可自訂外觀的控制項

適用於