Question

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.

Was it helpful?

Solution

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" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top