首页 > 解决方案 > 如何从 System.Windows.SystemColors.WindowTextBrushKey 更改为字符串颜色名称?

问题描述

有没有办法从后面的 WPF 代码中的System.Windows.SystemColors.WindowTextBrushKey获取颜色名称,如红色、黑色等?

       string color = "Black";
       if (System.Windows.SystemParameters.HighContrast)
       {                    
          color = System.Windows.SystemColors.WindowTextBrushKey; // I want to get color from this value
       }

标签: c#wpfsystemcolorshigh-contrast

解决方案


这取决于。SystemColors.WindowTextBrush会给你Brush。然后,您可以检查它的字符串表示是否匹配Brushes从类的静态属性返回的任何内容Brushes

string color = "Black";
if (System.Windows.SystemParameters.HighContrast)
{
    System.Windows.Media.Brush brush = System.Windows.SystemColors.WindowTextBrush;
    string s = brush.ToString();
    color = typeof(System.Windows.Media.Brushes).GetProperties()
        .FirstOrDefault(p => p.GetValue(null)?
        .ToString() == s)?
        .Name;
}

推荐阅读