BorderClipConverter.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Globalization;
  3. using System.Windows;
  4. using System.Windows.Data;
  5. using System.Windows.Media;
  6. namespace HandyControl.Tools.Converter;
  7. public class BorderClipConverter : IMultiValueConverter
  8. {
  9. public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  10. {
  11. if (values.Length == 3 && values[0] is double width && values[1] is double height && values[2] is CornerRadius radius)
  12. {
  13. if (width < double.Epsilon || height < double.Epsilon)
  14. {
  15. return Geometry.Empty;
  16. }
  17. var clip = new PathGeometry
  18. {
  19. Figures = new PathFigureCollection
  20. {
  21. new(new Point(radius.TopLeft, 0), new PathSegment[]
  22. {
  23. new LineSegment(new Point(width - radius.TopRight, 0), false),
  24. new ArcSegment(new Point(width, radius.TopRight), new Size(radius.TopRight, radius.TopRight), 90, false, SweepDirection.Clockwise, false),
  25. new LineSegment(new Point(width, height - radius.BottomRight), false),
  26. new ArcSegment(new Point(width - radius.BottomRight, height), new Size(radius.BottomRight, radius.BottomRight), 90, false, SweepDirection.Clockwise, false),
  27. new LineSegment(new Point(radius.BottomLeft, height), false),
  28. new ArcSegment(new Point(0, height - radius.BottomLeft), new Size(radius.BottomLeft, radius.BottomLeft), 90, false, SweepDirection.Clockwise, false),
  29. new LineSegment(new Point(0, radius.TopLeft), false),
  30. new ArcSegment(new Point(radius.TopLeft, 0), new Size(radius.TopLeft, radius.TopLeft), 90, false, SweepDirection.Clockwise, false),
  31. }, false)
  32. }
  33. };
  34. clip.Freeze();
  35. return clip;
  36. }
  37. return DependencyProperty.UnsetValue;
  38. }
  39. public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
  40. {
  41. throw new NotSupportedException();
  42. }
  43. }