动画应用于共享静态资源

时间:2014-08-21 08:14:52

标签: wpf wpf-animation wpf-style

我有RibbonButton元素,它使用StaticResource背景画笔。

现在我正在尝试将动画应用于此按钮的背景颜色,但会发生的是使用相同StaticResource的所有元素的背景颜色也都是动画的!

如何使此动画仅适用于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>

1 个答案:

答案 0 :(得分:1)

显然,当一个元素使用StaticResource时,该资源的相同实例将在使用它的所有元素之间共享。

作为解决方案,我在x:Shared="False"上使用了StaticResource标记属性,但它确实有效。

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