| 1234567891011121314151617181920212223242526272829303132333435 |
- 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";
- }
- }
- }
|