WPF Window.Resources VS Application.Resources

129 Views Asked by At

I created some animation on my style of "MenuItem" in Window.Resources

        <Style TargetType="MenuItem">
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="FontSize" Value="10"/>
        <Setter Property="Background">
            <Setter.Value>
                <SolidColorBrush Color="#2e3137"/>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type MenuItem}">
                    <Border x:Name="Bd" Padding="17,0,17,0" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="#2e3137" SnapsToDevicePixels="True" Uid="Border_38">
                        <ContentPresenter x:Name="ContentPresenter"  Content="{TemplateBinding Header}" Grid.Column="1" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Uid="ContentPresenter_33"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="MenuItem.MouseEnter">
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation To="Gray" Storyboard.TargetProperty="(MenuItem.Background).(SolidColorBrush.Color)" Duration="0:0:0.3"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="MenuItem.MouseLeave">
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation To="Transparent" Storyboard.TargetProperty="(MenuItem.Background).(SolidColorBrush.Color)" Duration="0:0:0.3"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Opacity" TargetName="Bd" Value="0.56"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

This one works well. But then i realized that i need to move style to Application.Resources, because i need this style in different windows, but when i moved it, animation stops working with error: 'Background' property does not point to a DependencyObject in path '(0).(1)'. So what the difference between this resources and how to solve my problem? Thx.

UPD: Found that problem not with MenuItems but with Labels, that strange becase i only set this style to Labels

    <Style TargetType="Label">
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="FontSize" Value="14"/>
    </Style>
0

There are 0 best solutions below