Search code examples
c#wpfxamlwindow

Why does style TargetType="Window" not work when set from App.xaml?


I'm creating a simple WPF project in VS2013 and I want to apply properties to my main Window. I set them in my App.xaml file like this:

<Application.Resources>
    <Style TargetType="Window">
        <Setter Property="Background" Value="#FF2D2D30" />
    </Style>
</Application.Resources>

The problem is that nothing happens. When I change the TargetType to Grid however, the setter property works just fine. Why does this happen?


Solution

  • It is necessary to add construction in Window:

    Style="{StaticResource {x:Type Window}}"
    

    Window in XAML:

    <Window x:Class="WindowStyleHelp.MainWindow"
            Style="{StaticResource {x:Type Window}}"
            ...>
    

    Or define Style in resources like this:

    xmlns:local="clr-namespace:MyWpfApplication"
    
    <Application.Resources>
        <Style TargetType="{x:Type local:MainWindow}">
            <Setter Property="Background" Value="#FF2D2D30"/>
        </Style>
    </Application.Resources>