SolidColorBrush + Color
using System.Windows.Media;
/// <summary>
/// 画刷-红色
/// </summary>
public static SolidColorBrush BackgroundColorRed = new SolidColorBrush(Colors.Red);
自定义颜色转换
前端
<Grid Background="#ffffff" x:Name="brdStateBackground" />
后台
using System.Windows.Media;
brdStateBackground.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ffffff"));
//string转Color
(Color)ColorConverter.ConvertFromString((string)str);
"#FFFFFF"转Color
public Color StringToColor(string colorName)
{
if (colorName.StartsWith("#"))
{
colorName = colorName.Replace("#", string.Empty);
}
if (int.TryParse(colorName, System.Globalization.NumberStyles.HexNumber, null, out int value))
{
return Color.FromArgb(
//Convert.ToByte((value >> 24) & 255), // a 默认为255,即不透明
Convert.ToByte((value >> 16) & 255), // R
Convert.ToByte((value >> 8) & 255), // G
Convert.ToByte((value >> 0) & 255) // B
);
}
return Color.Red;
}