当前位置 : 主页 > 网络编程 > c#编程 >

C# wpf Brush转Hex字符串的实例代码

来源:互联网 收集:自由互联 发布时间:2021-05-09
我就废话不多说了,大家还是直接看代码吧~ //from MaterialDesignDemo.Converters public class BrushToHexConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

我就废话不多说了,大家还是直接看代码吧~

//from MaterialDesignDemo.Converters
 public class BrushToHexConverter : IValueConverter
 {
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
   if (value == null) return null;
   string lowerHexString(int i) => i.ToString("X2").ToLower();
   var brush = (SolidColorBrush)value;
   var hex = lowerHexString(brush.Color.R) +
      lowerHexString(brush.Color.G) +
      lowerHexString(brush.Color.B);
   return "#" + hex;
  }
  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
   throw new NotImplementedException();
  }
 }

补充:C# 16进制转 Brush 颜色对象

代码图如下:

代码片如下:

BrushConverter brushConverter = new BrushConverter();
    PopupBorder.Background = (Brush)brushConverter.ConvertFromString("#121212");
    MessageLabel.Foreground = (Brush)brushConverter.ConvertFromString("#7d7d7d");

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。如有错误或未考虑完全的地方,望不吝赐教。

网友评论