当前位置: 动力学知识库 > 问答 > 编程问答 >

wpf - 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>

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: