この例では、プロパティ値が変更されたときに Trigger を使用して Storyboard を開始する方法を示します。 Style、ControlTemplate、または DataTemplate内で Trigger を使用できます。
例
次の例では、Trigger を使用して、IsMouseOver プロパティが true
になったときに、Button の Opacity をアニメーション化します。
<!-- 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) します。
関連項目
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET Desktop feedback