简体   繁体   中英

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

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!

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM