Search code examples
wpfwpf-animationwpf-style

Animation applied to shared static resource


I have RibbonButton element which uses a StaticResource background brush.

Now I am trying to apply animation to the background color of this button, but what happens is all the elements that uses the same StaticResource have their background color animated too !!!

How to make this animation applicable only to the RibbonButton ?

<Ribbon>
    <RibbonTab Header="Ribbon Tab 1">
        <RibbonGroup Header="Ribbon Group 1" Background="{StaticResource BackgroundBrush}">
            <RibbonButton x:Name="RibbonButton1" Label="Ribbon Button 1"
                          VerticalAlignment="Bottom" 
                          Style="{DynamicResource RibbonButton1_RibbonButtonStyle}"
                          Background="{StaticResource BackgroundBrush}">
                <RibbonButton.Resources>
                    <Style x:Key="RibbonButton1_RibbonButtonStyle" 
                           TargetType="RibbonButton">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding SomethingChanged}" Value="true">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ColorAnimation Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" 
                                                            Duration="0:0:0.1" To="Gold" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>
                                <DataTrigger.ExitActions>
                                    <BeginStoryboard>
                                        <Storyboard FillBehavior="Stop">
                                            <ColorAnimation Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" 
                                                            Duration="0:0:0.1" To="{StaticResource BackgroundColor}" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.ExitActions>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </RibbonButton.Resources>
            </RibbonButton>
        </RibbonGroup>
        ....
    </RibbonTab>
</Ribbon>

Solution

  • So apparently when an element uses a StaticResource, the same instance of that resource is shared between all the elements that uses it.

    So as a solution, I used the x:Shared="False" markup attribute on my StaticResource and it worked.

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
        <Color x:Key="BackgroundColor" A="255" R="250" G="250" B="250" />
    
        <SolidColorBrush x:Key="BackgroundBrush" x:Shared="False" Color="{StaticResource BackgroundColor}" />
    </ResourceDictionary>