如何在 WPF 中设置按钮的背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33213025/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:29:03  来源:igfitidea点击:

How to set background color of button in WPF?

c#wpfxamldevexpress

提问by Matthias Wolf

How to set the background color of a button in xaml?

如何在xaml中设置按钮的背景颜色?

It can't be much harder than

没有比这更难的了

<Button Margin="2" Background ="LightGreen" ....>

But this does not work...for the avoidance of confusion, button here is System.Windows.Controls.Button

但这不起作用......为避免混淆,这里的按钮是 System.Windows.Controls.Button

Edit

编辑

I forgot to mention that I use DevExpress's ThemeManager but did not think that would cause issues as per DevExpress they do not style the standard Windows Buttons...apparently they do, however, which basically makes it impossible to change the background color of a button without some major work...

我忘了提到我使用 DevExpress 的 ThemeManager 但我不认为这会导致问题,因为 DevExpress 他们没有设计标准的 Windows 按钮......然而,显然他们这样做了,这基本上不可能改变按钮的背景颜色没有一些主要的工作......

回答by nempoBu4

According to documentation:

根据文档

DevExpress provides multiple themes that can be applied to all DevExpress controls for WPF and some standard controls (GroupBox, ScrollViewer, Scroll, RadoiButton, Button, ListBox, Slider, TabControl, Separator, ToggleButton, RepeatButton, Label, ListBoxItem, TabItem, ToolTip, etc).

DevExpress 提供了多个主题,可以应用于 WPF 的所有 DevExpress 控件和一些标准控件(GroupBox、ScrollViewer、Scroll、RadoiButton、Button、ListBox、Slider、TabControl、Separator、ToggleButton、RepeatButton、Label、ListBoxItem、TabItem、ToolTip 等)。

As you can see the Buttoncontrol is listed here. But then in documentation it is said that you can disable theme of individual control by setting ThemeNameattribute to None. So, you can just disable the theme for button or for some of its parent containers and use your own style.
Here is example:

如您所见,Button此处列出了控件。但是在文档中据说您可以通过将ThemeName属性设置为None. 因此,您可以仅禁用按钮或其某些父容器的主题并使用您自己的样式。
这是示例:

<Button Margin="2" Background="LightGreen" dx:ThemeManager.ThemeName="None" ...>

回答by Salah Akbari

Try this:

尝试这个:

yourBtn.Background = new SolidColorBrush(Colors.Green);

Or in XAML:

或者在 XAML 中:

<Window.Resources>
   <SolidColorBrush x:Key="BG" Color="Green"/>
</Window.Resources>

<Button Background="{DynamicResource BG}"></Button>

回答by simonalexander2005

<Button Margin="2" Background="LightGreen"/>works. If it doesn't work for you, perhaps you have another styleor style manager overriding it somewhere.

<Button Margin="2" Background="LightGreen"/>作品。如果它对您不起作用,也许您有另一个style或样式管理器在某处覆盖它。

Additional information - Another answer shows how to style an individual button using dynamic resources. To set the background colour for all buttons in a style using a resourcedictionary, you can use something like:

附加信息 - 另一个答案显示了如何使用动态资源设置单个按钮的样式。要使用资源字典为样式中的所有按钮设置背景颜色,您可以使用以下内容:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!--background brush-->
    <LinearGradientBrush x:Key="backgroundBrush" StartPoint="0.5,0" EndPoint="0.5,1">
        <GradientStop Color="White" Offset="0.3"/>
        <GradientStop Color="#DDDDFF" Offset="1"/>
    </LinearGradientBrush>

        <!--The style for buttons-->
        <Style TargetType="Button">
            <Setter Property="Background" Value="{StaticResource backgroundBrush}"/>
        </Style>
    </ResourceDictionary>