「Androidは電気羊の夢を見るか」を読みたい管理者のブログ

仕事などでの色々な発見を記事にしてます。不定期更新。

DataTriggerとTriggerの違いがよくわからん

方法 : プロパティ値が変化したときにアニメーションをトリガーする
方法 : データが変化したときにアニメーションをトリガする
ん?と思うじゃろ。人目みただけで「プロパティとデータの違いって何?」って思うじゃろ
思わん君は色盲か、それとも目が節穴か

というわけなので実験してみたのです。

    <UserControl.Resources>
        <Storyboard x:Key="rectAnimation">
            <DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.25" Duration="0:0:1"/>
        </Storyboard>
        <Style x:Key="PropertTriggerExampleButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Opacity" Value="1"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Trigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource rectAnimation}"/>
                    </Trigger.EnterActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>
    <StackPanel>
        <Button x:Name="rect" Style="{StaticResource PropertTriggerExampleButtonStyle}" Content="move the mouse over me"/>
    </StackPanel>

このソースコードのTriggerとなってるところをDataTriggerにする
怒られました。そんなPropertyはないと怒られました。

で、何が問題になってるの?

WPF4.5入門 その48 「WPFのアニメーション その1」 - かずきのBlog@hatena

こちらのページで紹介されてるサンプルソース、EventTriggerをDataTriggerに入れ替えると怒られてしまうのですよ(続き書きました。解決編へ:
TriggerとAnimationを組み合わせる もしくは DataTriggerとTriggerて何が違うのよって話 - 「Androidは電気羊の夢を見るか」を読みたい管理者のブログ
)。

調べてみました。
DataTrigerもTriggerもTriggerBaseクラスを継承している
なるほど!

で、TriggerBaseを継承しているクラスは他にどんなのがあるの?

  • Trigger This is the simplest form of trigger. It watches for a change in a dependency property

and then uses a setter to change the style.

  • MultiTrigger This is similar to Trigger but combines multiple conditions. All the conditions must

be met before the trigger springs into action.

  • DataTrigger This trigger works with data binding. It’s similar to Trigger, except it watches for a

change in any bound data.

  • MultiDataTrigger This combines multiple data triggers.

EventTrigger This is the most sophisticated trigger. It applies an animation when an event occurs.


Amazon.co.jp: Pro Wpf 4.5 in C#: Windows Presentation Foundation in .net 4.5 (Professional Apress): Matthew MacDonald: 洋書

なるほど。

単なるTriggerとMultiTriggerがあり、DataTriggerとEventTriggerがある

わかったような分からないような

5つあるんですね。

で、私ひとつ勘違いしていました。

トリガ オブジェクトがアクティブになったときに適用する、TriggerAction オブジェクトのコレクションを取得します。
TriggerBase.EnterActions プロパティ (System.Windows)

なんですとー。
発火タイミングはオブジェクトがアクティブになった瞬間だったのかー!

うーむうーむ
まだまだ知らないことがたくさんありそうです。