为什么窗口背景的样式设置不起作用? [英] Why doesn't style-setting of the window background work?

查看:91
本文介绍了为什么窗口背景的样式设置不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是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>

我也有MainWindow.xaml.在VS中以设计"模式查看时,其背景确实应为灰色.无论如何,当应用程序运行时,窗口的背景是默认的白色.

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.

为什么?

如何解决此问题?我希望所有窗口默认都具有标准背景.

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

推荐答案

根据 CodeNaked 的答案,您'必须为每个拥有的Window创建一个Style,但是您可以对所有这些BasedOn使用相同的样式

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

这篇关于为什么窗口背景的样式设置不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆