我有一個數據對象 - 一個名爲Notification
的自定義類,它公開了IsCritical
屬性。這個想法是,如果通知過期,它有一段有效期,用戶應該注意它。WPF - 使動畫的執行以綁定數據項的屬性爲條件
想象的情景與此測試數據:
_source = new[] {
new Notification { Text = "Just thought you should know" },
new Notification { Text = "Quick, run!", IsCritical = true },
};
第二項應出現在ItemsControl
用脈衝背景。這裏有一個簡單的數據模板摘錄,顯示了我想要在灰色和黃色之間動畫背景的方法。
<DataTemplate DataType="Notification">
<Border CornerRadius="5" Background="#DDD">
<Border.Triggers>
<EventTrigger RoutedEvent="Border.Loaded">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="Background.Color"
From="#DDD" To="#FF0" Duration="0:0:0.7"
AutoReverse="True" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Border.Triggers>
<ContentPresenter Content="{TemplateBinding Content}" />
</Border>
</DataTemplate>
我不確定的是如何使這個動畫有條件的值爲IsCritical
。如果綁定值爲false
,則應保留#DDD
的默認背景色。
@Anvanka - 謝謝你。我之前沒有使用過DataTrigger EnterActions或ExitActions。還要感謝這個詳細的例子 - 一個很好的答案,值得獎勵。 – 2009-09-02 17:20:12
不客氣:)。我很高興能夠提供幫助。 – Anvaka 2009-09-02 20:43:59