StringToColorConverter.cs 1004 B

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Globalization;
  3. using System.Windows.Data;
  4. using System.Windows.Media;
  5. namespace SWRIS.Converters
  6. {
  7. public class StringToColorConverter : IValueConverter
  8. {
  9. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  10. {
  11. if (value is string colorString && !string.IsNullOrEmpty(colorString))
  12. {
  13. try
  14. {
  15. return (Color)ColorConverter.ConvertFromString(colorString);
  16. }
  17. catch
  18. {
  19. return Colors.Black; // 默认颜色
  20. }
  21. }
  22. return Colors.Black;
  23. }
  24. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  25. {
  26. if (value is Color color)
  27. {
  28. return $"#{color.R:X2}{color.G:X2}{color.B:X2}";
  29. }
  30. return "#000000";
  31. }
  32. }
  33. }