【发布时间】:2019-12-22 14:44:49
【问题描述】:
虽然一次性背景更改很容易并且已经有很多answers,但我如何才能始终如一地为颜色设置动画更改(例如,不是一次,不是单一颜色)。
举一个问题的玩具示例,假设我有一个 Rectangle 和三个名为“红色”、“蓝色”和“绿色”的 RadioButtons,如何为矩形的填充颜色设置动画,使其对应于选中单选按钮(例如,当您单击“蓝色”并且矩形变为蓝色时)?
<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>
我现在面临的问题是,每当Rectangle 的填充颜色附加多个动画时,由于动画优先级,它会以各种方式惨遭失败,例如,如果有多个触发器“红色”、“绿色”和“蓝色”:
<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>
当填充为蓝色时单击“绿色”,它将保持蓝色(因为“ToBlue”定义在“ToGreen”下方)。更糟糕的是,如果您尝试事先删除“ToBlue”:
<RemoveStoryboard BeginStoryboardName="ToBlue"/>
它会从默认颜色变为绿色,而不是从蓝色变为绿色。
【问题讨论】:
-
加分项:即使在动画进行时目标颜色发生变化,它也会持续工作。
-
@Frenchy 您建议的具有 RadioButtons 的“推送”语义的解决方案对我来说是有效的(不是我投票)。虽然毫无疑问
VisualStateManager更优雅,因为它允许更轻松的绑定。
标签: c# wpf xaml animation wpf-animation