WPF:情节提要样式,返回“不能在不可变对象实例上为'Color'设置动画”。 [英] WPF: Storyboard in style returning a "Cannot animate 'Color' on an immutable object instance."

查看:126
本文介绍了WPF:情节提要样式,返回“不能在不可变对象实例上为'Color'设置动画”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XAML:

<UserControl.Resources>
    <SolidColorBrush x:Key="Brush"></SolidColorBrush>

    <Style TargetType="{x:Type StackPanel}" x:Key="ColourChangingStyle">
        <Setter Property="Background" Value="{StaticResource Brush}" />

        <Style.Triggers>
            <DataTrigger Binding="{Binding Path='Stage'}" Value="1">
                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Storyboard.Target="{StaticResource Brush}"
                              Storyboard.TargetProperty="Color" From="{StaticResource FirstColor}" To="{StaticResource FinishedColor}" Duration="0:0:10" BeginTime="0:0:0"/>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

<StackPanel x:Name="InfoPanel" Orientation="Vertical"  Margin="1,2" Style="{DynamicResource ColourChangingStyle}">
 ...
</StackPanel>

我一直收到相同的错误,即:

And I keep getting the same error that:


无法在不可变对象实例上为颜色设置动画

Cannot animate 'Color' on an immutable object instance

这是画笔。我查找了这个问题,并认为这与将画笔绑定到 StackPanel 有关,这使得以后无法更改。

which is the brush. I looked up the problem, and believe that it's something to do with the binding the brush to the StackPanel making it unavailable to alter later.

有没有解决的办法,我真的不知道要实现相同效果的其他选择是不对颜色进行硬编码,也不执行代码中的所有事件。

Is there any way around this, I literally have no clue what my other options for the same effect are without hardcoding colors in, and doing all the events in code.

推荐答案

似乎您无法为资源画笔制作动画。 c>和 Storyboard.Target 。但是,您可以设置 Style Background (您已经完成此操作)并为其设置动画。

It seems that you can not animate a Brush in Resources with Storyboard.Target. But you can set the Background of your Style (you have done this already) and animate this property.

<ColorAnimation Storyboard.TargetProperty="Background.Color"
                From="{StaticResource FirstColor}"
                To="{StaticResource FinishedColor}" 
                Duration="0:0:10" BeginTime="0:0:0" />

这篇关于WPF:情节提要样式,返回“不能在不可变对象实例上为'Color'设置动画”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆