BorderElement.cs 2.2 KB

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