RectangleAttach.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.Windows;
  2. using System.Windows.Data;
  3. using System.Windows.Shapes;
  4. using HandyControl.Data;
  5. using HandyControl.Tools.Converter;
  6. namespace HandyControl.Controls;
  7. public class RectangleAttach
  8. {
  9. public static readonly DependencyProperty CircularProperty = DependencyProperty.RegisterAttached(
  10. "Circular", typeof(bool), typeof(RectangleAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnCircularChanged));
  11. private static void OnCircularChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  12. {
  13. if (d is Rectangle rectangle)
  14. {
  15. if ((bool) e.NewValue)
  16. {
  17. var binding = new MultiBinding
  18. {
  19. Converter = new RectangleCircularConverter()
  20. };
  21. binding.Bindings.Add(new Binding(FrameworkElement.ActualWidthProperty.Name) { Source = rectangle });
  22. binding.Bindings.Add(new Binding(FrameworkElement.ActualHeightProperty.Name) { Source = rectangle });
  23. rectangle.SetBinding(Rectangle.RadiusXProperty, binding);
  24. rectangle.SetBinding(Rectangle.RadiusYProperty, binding);
  25. }
  26. else
  27. {
  28. BindingOperations.ClearBinding(rectangle, FrameworkElement.ActualWidthProperty);
  29. BindingOperations.ClearBinding(rectangle, FrameworkElement.ActualHeightProperty);
  30. BindingOperations.ClearBinding(rectangle, Rectangle.RadiusXProperty);
  31. BindingOperations.ClearBinding(rectangle, Rectangle.RadiusYProperty);
  32. }
  33. }
  34. }
  35. public static void SetCircular(DependencyObject element, bool value)
  36. => element.SetValue(CircularProperty, ValueBoxes.BooleanBox(value));
  37. public static bool GetCircular(DependencyObject element)
  38. => (bool) element.GetValue(CircularProperty);
  39. }