C# WPF Designer Exception: animation object cannot

2019-04-08 08:19发布

问题:

This question already has an answer here:

  • Color Animation ContentPresenter 1 answer

The following code runs and works perfectly at runtime but crashes the designer and I have NO idea why.

<VisualState x:Name="Selected" >
    <Storyboard>
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="HeaderTopSelected">
            <EasingColorKeyFrame KeyTime="0" Value="White"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

That crashes the designer with the following error:

'System.Windows.Media.Animation.ColorAnimationUsingKeyFrames' animation object cannot be used to animate property 'Foreground' because it is of incompatible type 'System.Windows.Media.Brush'

I have been trying to figure this out for hours now and I just have no idea why this crashing the designer considering this example is shown many times online and works at run-time.

Can anyone please tell me what I am doing wrong?

Thanks!

回答1:

You might have to use this syntax:

Storyboard.TargetProperty="(TextBlock.Foreground).Color"

"Foreground" and "SolidColorBrush" are the same object.