using System; using System.Globalization; using System.Windows.Data; using System.Windows.Media; namespace SWRIS.Converters { public class StringToColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is string colorString && !string.IsNullOrEmpty(colorString)) { try { return (Color)ColorConverter.ConvertFromString(colorString); } catch { return Colors.Black; // 默认颜色 } } return Colors.Black; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (value is Color color) { return $"#{color.R:X2}{color.G:X2}{color.B:X2}"; } return "#000000"; } } }