【问题标题】:C# WPF set system color to buttonC# WPF 将系统颜色设置为按钮
【发布时间】:2013-11-29 04:51:04
【问题描述】:

我想将按钮的背景颜色更改为系统颜色。就像我正在更改按钮颜色一样

button2.Background = Brushes.Blue; 

到蓝色。但是不,我想将颜色更改为系统颜色。

【问题讨论】:

  • 有很多不同的系统颜色。你要哪一个?

标签: c# .net wpf


【解决方案1】:

您可以从 System.Windows.SystemColors 获取系统颜色

button2.Background = System.Windows.SystemColors.MenuHighlightBrush;

WPF 背景的类型是 System.Windows.Media.Brush。您可以像这样设置另一种颜色:

// using System.Windows.Media;

button2.Background = Brushes.White;

button2.Background = new SolidColorBrush(Color.White);

button2.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0, 0);

【讨论】:

    【解决方案2】:

    希望对你有帮助

    <Button 
          Background="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}" 
          Content="Hello, World!" />
    

    【讨论】:

      【解决方案3】:

      你可以试试这样的:

      button2.Background = SystemColors.ActiveCaptionBrush;
      

      SystemColors 属于 System.Windows 命名空间。

      【讨论】:

      • 它没有给出实际的系统颜色。我希望它从主题中挑选
      【解决方案4】:

      背景是画笔类型。但是您可以使用系统颜色创建一个实心画笔并使用它。

      SolidBrush systemColorBrush = new SolidBrush(systemColor); // Create SolidBrush from color
      button2.Background = systemColorBrush;
      

      【讨论】:

      • solidbrush 在 WPF 中给我错误,但它在 Windows 窗体中完美运行。
      猜你喜欢
      • 2017-01-12
      • 1970-01-01
      • 2017-12-18
      • 2013-08-30
      • 2016-01-14
      • 2015-06-27
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      相关资源
      最近更新 更多