1. 程式人生 > 程式設計 >C# wpf Brush轉Hex字串的例項程式碼

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,CultureInfo culture)
  {
   throw new NotImplementedException();
  }
 }

補充:C# 16進位制轉 Brush 顏色物件

程式碼圖如下:

C# wpf Brush轉Hex字串的例項程式碼

C# wpf Brush轉Hex字串的例項程式碼

程式碼片如下:

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

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