虽然单次更改背景很容易,而且已经有许多答案(链接),但我如何使颜色动画变化保持一致(例如不只一次,也不是只到单一颜色)?
为了给出一个玩具示例,假设我有一个名为“红色”、“蓝色”和“绿色”的RadioButton
,并拥有一个Rectangle
。 我该如何使Rectangle
填充色的动画对应选中的单选按钮(例如,每当您点击“蓝色”时,Rectangle
变成蓝色)?
<StackPanel HorizontalAlignment="Left">
<Rectangle Width="50" Height="50" Style="{StaticResource ColorChangingStyle}"/>
<RadioButton Name="Red" Content="Red" GroupName="Group1" IsChecked="True"/>
<RadioButton Name="Green" Content="Green" GroupName="Group1"/>
<RadioButton Name="Blue" Content="Blue" GroupName="Group1"/>
</StackPanel>
我现在面临的问题是,每当有多个动画附加到“矩形”填充颜色时,由于动画优先级,它会以各种方式惨败,例如如果有多个“红色”,“绿色”和“蓝色”的触发器:
<DataTrigger Binding="{Binding IsChecked, ElementName=Blue}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="ToBlue">
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" To="Blue" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
如果在“填充”为蓝色的情况下点击“绿色”,它将保持蓝色(因为“转换为蓝色”在“转换为绿色”之后定义)。更糟糕的是,如果您先尝试删除“转换为蓝色”:
<RemoveStoryboard BeginStoryboardName="ToBlue"/>
它将从默认颜色动画到绿色,而不是从蓝色到绿色。