1

我有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>
4

1 回答 1

1

所以很明显,当一个元素使用 a 时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>
于 2014-08-21T08:44:48.307 回答