40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Windows.Data;
|
|
using System.Windows.Media;
|
|
|
|
namespace HandyControl.Tools.Converter
|
|
{
|
|
public class Color2HexStrConverter : IValueConverter
|
|
{
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (value is SolidColorBrush brush)
|
|
{
|
|
var color = brush.Color;
|
|
return color.A < 255 ? $"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}" : $"#{color.R:X2}{color.G:X2}{color.B:X2}";
|
|
}
|
|
return "";
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (value is string str)
|
|
{
|
|
try
|
|
{
|
|
if (ColorConverter.ConvertFromString(str) is Color color)
|
|
{
|
|
return new SolidColorBrush(color);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return new SolidColorBrush(default(Color));
|
|
}
|
|
return new SolidColorBrush(default(Color));
|
|
}
|
|
return new SolidColorBrush(default(Color));
|
|
}
|
|
}
|
|
} |