BorderCircularClipConverter.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. //referenced from https://stackoverflow.com/a/5650367/9639378
  2. using System;
  3. using System.Globalization;
  4. using System.Windows;
  5. using System.Windows.Data;
  6. using System.Windows.Media;
  7. namespace HandyControl.Tools.Converter;
  8. public class BorderCircularClipConverter : IMultiValueConverter
  9. {
  10. public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  11. {
  12. if (values.Length == 3 && values[0] is double width && values[1] is double height && values[2] is CornerRadius radius)
  13. {
  14. if (width < double.Epsilon || height < double.Epsilon)
  15. {
  16. return Geometry.Empty;
  17. }
  18. var clip = new RectangleGeometry(new Rect(0, 0, width, height), radius.TopLeft, radius.TopLeft);
  19. clip.Freeze();
  20. return clip;
  21. }
  22. return DependencyProperty.UnsetValue;
  23. }
  24. public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
  25. {
  26. throw new NotSupportedException();
  27. }
  28. }