Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't style-setting of the window background work?

Tags:

wpf

xaml

app.xaml

Here is the App.xaml:

<Application>
<Application.Resources>
    <ResourceDictionary>
        <Style TargetType="Window">
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
        </Style>
    </ResourceDictionary>
</Application.Resources>
</Application>

I also have the MainWindow.xaml. When viewed in Design mode in VS, it's background is, indeed, gray, as it should be. Anyway, when application is running, the window's background is default white.

Why?

How to fix this? I want all windows to have the standard background by default.

like image 561
Mikhail Orlov Avatar asked Aug 01 '11 17:08

Mikhail Orlov


2 Answers

Following up on the answer from CodeNaked, you'll have to create a Style for every Window you have, but you can use the same style for all of them with BasedOn like this

<Application.Resources>
    <ResourceDictionary>
        <Style TargetType="Window">
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
        </Style>
        <Style TargetType="{x:Type local:MainWindow}"
               BasedOn="{StaticResource {x:Type Window}}"/>
        <Style TargetType="{x:Type local:SomeOtherWindow}"
               BasedOn="{StaticResource {x:Type Window}}"/>
        <!-- Add more Windows here... -->
    </ResourceDictionary>
</Application.Resources
like image 198
Fredrik Hedblad Avatar answered Sep 20 '22 20:09

Fredrik Hedblad


The problem is during runtime, the type of your window will be MainWindow, not Window. Implicit Styles are not applied to derived types of the TargetType. So your Style won't be applied.

During design time, you are designing your MainWindow, but I suspect it creates a Window as the basis.

You'd need to change the type to match your window's type.

like image 30
CodeNaked Avatar answered Sep 19 '22 20:09

CodeNaked