WPF: Storyboard in style returning a “Cannot animate 'Color' on an immutable object instance.”

ぐ巨炮叔叔 提交于 2019-12-12 09:44:04

问题


I have the following 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

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.


回答1:


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" />


来源:https://stackoverflow.com/questions/14383214/wpf-storyboard-in-style-returning-a-cannot-animate-color-on-an-immutable-obj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!