ColorToHoverBackgroundConverter.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Globalization;
  3. using System.Windows;
  4. using System.Windows.Data;
  5. using System.Windows.Media;
  6. namespace SWRIS.Converters
  7. {
  8. public class ColorStringToHoverBackgroundConverter : IValueConverter
  9. {
  10. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  11. {
  12. if (value is string colorString && !string.IsNullOrEmpty(colorString))
  13. {
  14. try
  15. {
  16. // 移除#号并确保6位十六进制
  17. string hex = colorString.TrimStart('#');
  18. if (hex.Length == 6)
  19. {
  20. return $"#1A{hex}"; // 添加透明度
  21. }
  22. }
  23. catch
  24. {
  25. return DependencyProperty.UnsetValue;
  26. }
  27. }
  28. return DependencyProperty.UnsetValue;
  29. }
  30. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  31. {
  32. throw new NotImplementedException();
  33. }
  34. }
  35. public class ColorStringToHoverBorderBrushConverter : IValueConverter
  36. {
  37. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  38. {
  39. if (value is string colorString && !string.IsNullOrEmpty(colorString))
  40. {
  41. try
  42. {
  43. string hex = colorString.TrimStart('#');
  44. if (hex.Length == 6)
  45. {
  46. return $"#4D{hex}"; // 添加透明度
  47. }
  48. }
  49. catch
  50. {
  51. return DependencyProperty.UnsetValue;
  52. }
  53. }
  54. return DependencyProperty.UnsetValue;
  55. }
  56. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  57. {
  58. throw new NotImplementedException();
  59. }
  60. }
  61. public class ColorStringToCheckedBackgroundConverter : IValueConverter
  62. {
  63. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  64. {
  65. if (value is string colorString && !string.IsNullOrEmpty(colorString))
  66. {
  67. try
  68. {
  69. string hex = colorString.TrimStart('#');
  70. if (hex.Length == 6)
  71. {
  72. return $"#4C{hex}"; // 添加透明度
  73. }
  74. }
  75. catch
  76. {
  77. return DependencyProperty.UnsetValue;
  78. }
  79. }
  80. return DependencyProperty.UnsetValue;
  81. }
  82. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  83. {
  84. throw new NotImplementedException();
  85. }
  86. }
  87. }