wpf 按钮的默认背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17365631/
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
Default background color of a button
提问by paparazzo
Want to match the default background color of a button for use in an Expander.
What is the default background color of a button?
想要匹配用于扩展器的按钮的默认背景颜色。
按钮的默认背景颜色是什么?
What have I tried to far?
A lot of different colors but did not find a match.
我尝试过什么?
很多不同的颜色,但没有找到匹配的。
回答by paparazzo
Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
回答by Smeegs
In WPF you can right click on an element and select "Edit Template", this will create the exact template that control is using including background color. You can then apply the same generated background color to whatever control you want.
在 WPF 中,您可以右键单击一个元素并选择“编辑模板”,这将创建控件使用的确切模板,包括背景颜色。然后,您可以将生成的相同背景颜色应用于您想要的任何控件。
Alternatively, you can go to that controls brush properties and click on the little square and select "Convert to new resource" if you're just looking to replicate a single brush. Then apply that newly generated brush to whatever element you want.
或者,如果您只想复制单个画笔,您可以转到该控件画笔属性并单击小方块并选择“转换为新资源”。然后将新生成的画笔应用于您想要的任何元素。
Resulting in the following
结果如下
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<LinearGradientBrush x:Key="Brush1" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF3F3F3" Offset="0"/>
<GradientStop Color="#FFEBEBEB" Offset="0.5"/>
<GradientStop Color="#FFDDDDDD" Offset="0.5"/>
<GradientStop Color="#FFCDCDCD" Offset="1"/>
</LinearGradientBrush>
</Window.Resources>
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="151,139,0,0" VerticalAlignment="Top" Width="75" Background="{DynamicResource Brush1}"/>
<Expander Header="Expander" HorizontalAlignment="Left" Margin="412,129,0,0" VerticalAlignment="Top" Background="{DynamicResource Brush1}">
<Grid Background="#FFE5E5E5"/>
</Expander>
</Grid>
回答by Avram Tudor
Here are the predefined brushes that wpf controls are using
You will still need to find which one is used by the Button control, you can do that by editing the Button template with Blend.
您仍然需要找到 Button 控件使用的是哪个,您可以通过使用 Blend 编辑 Button 模板来实现。
Depending on the case you might need to reference the brush as
根据情况,您可能需要将画笔引用为
"{DynamicResource {x:Static SystemColors.ControlLightLightColorKey}}"
or whatever the brush is.
或无论刷子是什么。
Edit1:If I'm not mistaken the WindowBrush resource is the one used by the Button control for its default background.
Edit1:如果我没记错的话,WindowBrush 资源是 Button 控件用于其默认背景的资源。
Edit2Don't forget that the default Button template also contains a chrome object that adds some impact on the final layout.
Edit2不要忘记默认的 Button 模板还包含一个 chrome 对象,它对最终布局增加了一些影响。
回答by saamorim
The WPF will apply different the colors/shapes depending on the operating system. So, there isn't exactly one default color. But, as for reference, check the button controltemplate page. You should find the static resources WindowBackgroundBrush, DisabledBackgroundBrush and SelectedBackgroundBrush
WPF 将根据操作系统应用不同的颜色/形状。因此,不存在完全一种默认颜色。但是,作为参考,请检查按钮控件模板页面。您应该找到静态资源 WindowBackgroundBrush、DisabledBackgroundBrush 和 SelectedBackgroundBrush
回答by Quantic
Blam's answer works but is a "dynamic reference to the control system brush", so in my case because my button is nested into a stackpanel that starts out disabled, when I used Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}", it caused my button to be a flat gray even when the stackpanel (and therefore button) were enabled. Apparently it dynamically set my button to the "disabled gray" color, but doesn't dynamically change it to the full color mode when enabled.
Blam 的回答有效,但它是“对控制系统画笔的动态引用”,所以在我的情况下,因为我的按钮嵌套在开始禁用的堆栈面板中,当我使用时Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}",它导致我的按钮变为纯灰色,即使堆栈面板(因此按钮)已启用。显然,它动态地将我的按钮设置为“禁用灰色”颜色,但在启用时不会将其动态更改为全彩模式。
As my link says, you can get a static reference to the button using:
正如我的链接所说,您可以使用以下方法获得对该按钮的静态引用:
Background="{DynamicResource {x:Static SystemColors.ControlBrush}}"
Background="{DynamicResource {x:Static SystemColors.ControlBrush}}"
Note the only difference is using ControlBrushinstead of ControlBrushKey. Doing this allowed my button to start out flat gray when disabled, but was full color when enabled.
请注意,唯一的区别是使用ControlBrush而不是ControlBrushKey。这样做允许我的按钮在禁用时以纯灰色开始,但在启用时为全彩色。
回答by VladH
you could use 'Colour to Html' it's a nice application and you have a tool to extract any color from you desktop Colour to Html
你可以使用“颜色到 Html”,这是一个不错的应用程序,你有一个工具可以从桌面上提取任何颜色颜色到 Html

