我有一个数据对象——一个名为Notification
的自定义类——其中暴露了一个IsCritical
属性。这意味着如果通知将要过期,它具有有效期,并且应该吸引用户的注意。
想象一个具有以下测试数据的场景:
_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
。