VisualStateManager.VisualStateGroups 添付プロパティ

定義

VisualStateGroup オブジェクトのコレクションを取得または設定します。

see GetVisualStateGroups
see GetVisualStateGroups
see GetVisualStateGroups

次の例では、1 つのControlTemplateを含むButtonの単純なGridを作成します。 また、VisualStateGroupCommonStatesの状態を定義する MouseOver という名前のNormalも含まれています。 VisualStateGroupには、ユーザーがマウス ポインターをVisualTransitionの上に移動したときに、Gridが緑から赤に変わるまで 1/2 秒かかることを指定する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が定義する 2 つの状態は、押されたとき ("Pressed") とそうでない場合 ("Normal") に対応します。

コントロールに VisualState を追加するには、コントロールの添付プロパティ VisualStateGroups 設定します。 相互に排他的な状態を同じ VisualStateGroupに配置します。 たとえば、 CheckBox には 2 つの VisualStateGroup オブジェクトがあります。 1 つには、状態、 NormalMouseOverPressed、および Disabledが含まれます。 もう 1 つには、状態、 CheckedUnChecked、および Indeterminateが含まれます。 CheckBoxは、MouseOver状態とUnChecked状態で同時に指定できますが、MouseOverPressedの状態に同時に存在することはできません。

VisualStateオブジェクトは任意の要素に追加できますが、他のユーザーがControlの視覚的な動作を再定義できるようにするために特に便利な方法です。 ControlTemplateを使用するカスタム コントロールを作成する場合は、そのクラス定義にTemplateVisualStateAttributeを追加することで、コントロールを使用できる状態を指定できます。 その後、コントロールの新しい ControlTemplate を作成したユーザーは、 VisualState オブジェクトをテンプレートに追加できます。 同じ System.Windows.TemplateVisualStateAttribute.GroupName を持つ状態は、同じ VisualStateGroupに属します。

VisualStateGroupControlTemplate オブジェクトを使用する方法の詳細については、「コントロールのテンプレートを作成する方法」を参照してください。 VisualStateManagerを使用するコントロールを作成する方法の詳細については、「カスタマイズ可能な外観を持つコントロールの作成」を参照してください。

適用対象