ThicknessSplitConverter.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Globalization;
  3. using System.Windows;
  4. using System.Windows.Data;
  5. namespace HandyControl.Tools.Converter;
  6. public class ThicknessSplitConverter : IValueConverter
  7. {
  8. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  9. {
  10. if (value is Thickness thickness)
  11. {
  12. if (parameter is string str)
  13. {
  14. var arr = str.Split(',');
  15. if (arr.Length != 4)
  16. {
  17. return thickness;
  18. }
  19. var result = new Thickness(thickness.Left, thickness.Top, thickness.Right, thickness.Bottom);
  20. if (double.TryParse(arr[0], out double leftTimes))
  21. {
  22. result.Left = leftTimes * thickness.Left;
  23. }
  24. if (double.TryParse(arr[1], out double topTimes))
  25. {
  26. result.Top = topTimes * thickness.Top;
  27. }
  28. if (double.TryParse(arr[2], out double rightTimes))
  29. {
  30. result.Right = rightTimes * thickness.Right;
  31. }
  32. if (double.TryParse(arr[3], out double bottomTimes))
  33. {
  34. result.Bottom = bottomTimes * thickness.Bottom;
  35. }
  36. return result;
  37. }
  38. }
  39. return value;
  40. }
  41. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  42. {
  43. throw new NotSupportedException();
  44. }
  45. }