WPF 窗口背景色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4968562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF windows background color
提问by Thomas
by default wpf window having white color. so what color i should specify for background as a result it will look like normal window which is just like .net 2.0 win apps windows color. please help
默认情况下,wpf 窗口具有白色。所以我应该为背景指定什么颜色,结果它看起来像普通窗口,就像 .net 2.0 win 应用程序窗口颜色。请帮忙
回答by Cody Gray
You need to paint the background using a system color brush.
您需要使用系统颜色画笔绘制背景。
The SystemColors.ControlBrushKey
propertywill return the ResourceKey
for the appropriate SolidColorBrush
.
该SystemColors.ControlBrushKey
属性将返回ResourceKey
适当的SolidColorBrush
。
For example, to set the background of a button, you might use the following code:
例如,要设置按钮的背景,您可以使用以下代码:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowTitle="SystemColors Example" Background="White">
<StackPanel Margin="20">
<Button
Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
Content="Hello, World!" />
</StackPanel>
</Page>
回答by Edward Brey
Dynamically bind the window's background to the "Control" brush from the system color palette. You can do that in your window class's constructor with this:
将窗口的背景动态绑定到系统调色板中的“控制”画笔。您可以在窗口类的构造函数中使用以下方法执行此操作:
SetResourceReference(BackgroundProperty, SystemColors.ControlBrushKey);