: 'Cannot animate 'Fill.Color' on an immutable object instance.'

社会主义新天地 提交于 2021-02-05 11:25:08

问题


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>

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?

Thank you!


回答1:


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>


来源:https://stackoverflow.com/questions/64705195/cannot-animate-fill-color-on-an-immutable-object-instance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!