: '无法在不可变对象实例上为 'Fill.Color' 设置动画. [英] : 'Cannot animate 'Fill.Color' on an immutable object instance.'
问题描述
我是 WPF 的新手,我正在尝试将形状设置为闪烁的红色".彩色动画,以防我得到一些触发值.这是我的相关 XAML 代码:
I'm new to WPF and I'm mm trying to animate a shape to a "Flashing red" color animation in case I get some trigger value. Here is my relevant XAML code:
<Rectangle Width="840" Height="40">
<Rectangle.Style>
<Style TargetType="Rectangle">
<Setter Property="Fill" Value="{Binding AlertUnit.AlertColor, FallbackValue=LightPink}"> </Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding AlertUnit.AlertString}" Value="EMERGENCY" >
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Fill.Color" To="White" Duration="0:0:2" AutoReverse="True"
RepeatBehavior="Forever"></ColorAnimation>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
但我收到以下错误:'Cannot animate 'Fill.Color' on an immutable object instance.'
我知道这是由于 Binding to the fill 属性,如果我将其更改为静态画笔颜色效果很好,但我需要根据某些逻辑更改颜色,所以有什么解决方法可以解决吗?
But I'm getting the following error: 'Cannot animate 'Fill.Color' on an immutable object instance.'
I know that it is due to the Binding to the fill property, If I change it to a static brush color it works well, but I need the color to change according to some logic so is there any workaround to solve it?
谢谢!
推荐答案
创建一个新的 SolidColorBrush 作为 Fill Setter 的值:
Create a new SolidColorBrush as value for the Fill Setter:
<Style TargetType="Rectangle">
<Setter Property="Fill">
<Setter.Value>
<SolidColorBrush Color="{Binding AlertUnit.AlertColor.Color}"/>
</Setter.Value>
</Setter>
...
</Style>
这篇关于: '无法在不可变对象实例上为 'Fill.Color' 设置动画.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!