Why doesn't style-setting of the window background work?
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.Control开发者_开发问答BrushKey}}"/>
</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.
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.
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
精彩评论