Milan
Milan

Reputation: 616

ColorAnimation doesn't work if TargetProperty is set using Converter?

I have a DateTemplate for my NoOneCares model, which is a simple Path.

Now, I want my Path to blink 3 times (Storyboard, ColorAnimation on Path.Fill), when the DataTrigger is triggered.

The example below crashes every time I trigger the DataTrigger:

System.InvalidOperationException: Cannot animate '(0).(1)' on an immutable object instance.

But when I set the Fill property directly Fill="Red" (and not with a ValueConverter), it works perfectly.

Why is this the case? I still need that Converter to set Fill to the correct color :/

 <DataTemplate DataType ="{x:Type models:NoOneCares}">
            <Path Stroke="White" Data="M 5,15 L5,10 L10,0 L 0,0 L 5,10" >

            <Path.Fill>
                <MultiBinding Converter="{StaticResource colorConverter}">
                    <Binding Path="NoOneCares"></Binding>
                    <Binding Path="NoOneCares"></Binding>
                </MultiBinding>
            </Path.Fill>

                <Path.Style>
                    <Style TargetType="Path">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding AnimationTrigger}" Value="{StaticResource numberTest}">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard Name="fsadf">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames AutoReverse="True" RepeatBehavior="3x" Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)">
                                                <LinearColorKeyFrame KeyTime="0:0:0.3"
                                             Value="White"/>
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Path.Style>
            </Path>
        </DataTemplate>

Upvotes: 1

Views: 200

Answers (1)

stop-cran
stop-cran

Reputation: 4408

Seems like the converter returns a freezed (immutable) brush, e.g. Brushes.Red. You can check this by IsFrozen property. Such objects cannot be changed.

You should clone the brush by calling Clone() method and return the cloned object - it's not frozen and can be changed.

Upvotes: 2

Related Questions