C# wpf Brush轉Hex字符串的實例代碼

我就廢話不多說瞭,大傢還是直接看代碼吧~

//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");

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀: