初学WPF,还有好多东西弄不懂,有些东西学的还很混乱。工作中需要一个可以闪烁的BUTTON,看上去挺简单的,做出来也很简单,但是实现的过程对我来说还是有点艰难。
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding Test}" Value="0">
<DataTrigger.EnterActions>
<BeginStoryboard Name="borderChangStoryboard">
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="BorderBrush.Color" From="Red" To="Pink" AutoReverse="True" RepeatBehavior="Forever"></ColorAnimation>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<StopStoryboard BeginStoryboardName="borderChangStoryboard"></StopStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
这个Style的设计是使用了DataTrigger,其中动画的部分稍有难度:
选择ColorAnimation与DoubleAnimation?
开始使用了DoubleAnimation,想用opacity控制变化,但是Storyboard.TargetName属性设置会出错,所幸就放弃了。
然后使用ColorAnimation,但还是有错,错误原因“colorAnimation动画对象不能用于动画属性BorderBrush”,上网查了下,Storyboard.TargetProperty="BorderBrush"改为Storyboard.TargetProperty="BorderBrush.Color",成功了!