次の方法で共有


方法: プロパティ値が変更されたときにアニメーションをトリガーする

この例では、プロパティ値が変更されたときに Trigger を使用して Storyboard を開始する方法を示します。 StyleControlTemplate、または DataTemplate内で Trigger を使用できます。

次の例では、Trigger を使用して、IsMouseOver プロパティが trueになったときに、ButtonOpacity をアニメーション化します。

<!-- PropertyTriggerExample.xaml
     Shows how to use property triggers to start animations. -->
<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  WindowTitle="Animate Properties with Storyboards">
  <Page.Resources>
  
    <Style x:Key="PropertyTriggerExampleButtonStyle" TargetType="{x:Type Button}">
      
      <Setter Property="Opacity" Value="0.25" />
      
      <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">

          <Trigger.EnterActions>
            <BeginStoryboard>
              <Storyboard>
                <DoubleAnimation Storyboard.TargetProperty="Opacity"
                  To="1" Duration="0:0:1" />
              </Storyboard>
            </BeginStoryboard>
          </Trigger.EnterActions>
          <Trigger.ExitActions>
            <BeginStoryboard>
              <Storyboard>
                <DoubleAnimation Storyboard.TargetProperty="Opacity"
                  To="0.25" Duration="0:0:1" />
              </Storyboard>
            </BeginStoryboard>
          </Trigger.ExitActions>          
        </Trigger>               
      </Style.Triggers>    
    </Style>
  </Page.Resources>

  <StackPanel Margin="20">

    <Button Style="{StaticResource PropertyTriggerExampleButtonStyle}">
      Move the mouse over me.
    </Button>

  </StackPanel>
</Page>

プロパティ Trigger オブジェクトによって適用されるアニメーションは、EventTrigger アニメーションや Storyboard メソッドを使用して開始されるアニメーションよりも、より複雑な方法で動作します。 それらは、他の Trigger オブジェクトによって定義されているアニメーションとは "ハンドオフ" しますが、EventTrigger およびメソッドでトリガーされたアニメーションとは複合 (compose) します。

関連項目