简体   繁体   English

如何将窗口背景绑定到主题的窗口背景颜色?

[英]How to bind Window background to a theme's Window background color?

How can I bind the default Window background color of the theme to a Window background? 如何将主题的默认Window背景颜色绑定到Window背景?

I've tried the following: 我尝试过以下方法:

<Window x:Class="Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Shell" 
    Height="300" 
    Width="300" 
    Background="{DynamicResource WindowBackgroundBrush}"
    >

But the background color renders as black and that does not seem right when using the Luna theme. 但是背景颜色呈现为黑色,使用Luna主题时似乎不正确。 Using the Luna theme I would expect the beige/brownish color often found in Windows XP. 使用Luna主题我会期望在Windows XP中经常出现米色/棕色。

UPDATE: After reading your answers, playing with StyleSnooper and thinking some more about this I've come to the following conclusion: 更新:在阅读完答案后,与StyleSnooper一起玩并思考更多相关内容后,我得出以下结论:

The Window will use SystemColors.WindowBrush as its background color by default. 默认情况下,Window将使用SystemColors.WindowBrush作为其背景颜色。 This color is white in most themes i've tried and that is probably OK. 在我尝试的大多数主题中,这种颜色是白色的,这可能是好的。 I believe white is also the Window background color in WinForms. 我相信白色也是WinForms中的Window背景色。 However, the default Form background color in WinForms is not Window background but Control. 但是,WinForms中的默认窗体背景颜色不是窗口背景而是控件。 So, if I want that color I have use SystemColors.ControlBrush as pointed out by Lucas . 所以,如果我想要那种颜色,我已经使用了Lucas所指出的SystemColors.ControlBrush

At one point I was trying to achieve the brownish/beige Control color of WinForms on Windows XP. 有一次,我试图在Windows XP上实现WinForms的褐色/米色控制颜色。 This is probably not possible on Windows Vista since Control color on Vista is kind of gray. 这在Windows Vista上可能无法实现,因为Vista上的控件颜色有点灰色。 Running a WinForms application on Windows Vista will not render it as beige/brownish, but gray. 在Windows Vista上运行WinForms应用程序不会将其呈现为米色/褐色,而是灰色。 In this case I guess we have to apply our own custom styles to achieve the "original" Windows XP look on Vista. 在这种情况下,我想我们必须应用我们自己的自定义样式来实现Vista上的“原始”Windows XP外观。

Thanks everyone for helping me sort this out! 谢谢大家帮助我解决这个问题!

Does applying a theme actually override the system brushes as I would expect? 应用主题实际上是否超出了我期望的系统画笔? If so, you should be able to do this: 如果是这样,您应该能够这样做:

<Window ...
    Background="{x:Static SystemColors.WindowBrush}
    ...>

Edit 编辑

As per Manga's comment, you'll probably want to use SystemColors.ControlBrush to achieve the desired effect, because WindowBrush on most systems is white. 根据Manga的评论,您可能希望使用SystemColors.ControlBrush来实现所需的效果,因为大多数系统上的WindowBrush都是白色的。 ControlBrush is the "off-white" colour you're seeing on Windows XP. ControlBrush是您在Windows XP上看到的“灰白”颜色。

I would have thought that simply leaving the Background property unset on your Window element would achieve this. 我原本以为只是在Window元素上保留Background属性就可以实现这一点。 The whole point of a theme is that it effects the default appearance of visual elements such as your Window . 主题的重点是它会影响视觉元素的默认外观,例如您的Window

EDIT You might look at StyleSnooper -- it will let you view the default template for a control (such as Window). 编辑您可以查看StyleSnooper - 它将允许您查看控件的默认模板(例如Window)。 You should be able to determine what property it is binding to for each theme. 您应该能够确定它为每个主题绑定的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM