AxleCanvas.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. namespace HandyControl.Controls;
  4. public class AxleCanvas : Canvas
  5. {
  6. public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
  7. nameof(Orientation), typeof(Orientation), typeof(AxleCanvas), new PropertyMetadata(default(Orientation)));
  8. public Orientation Orientation
  9. {
  10. get => (Orientation) GetValue(OrientationProperty);
  11. set => SetValue(OrientationProperty, value);
  12. }
  13. protected override Size ArrangeOverride(Size arrangeSize)
  14. {
  15. foreach (UIElement internalChild in InternalChildren)
  16. {
  17. if (internalChild == null) continue;
  18. var x = 0.0;
  19. var y = 0.0;
  20. if (Orientation == Orientation.Horizontal)
  21. {
  22. x = (arrangeSize.Width - internalChild.DesiredSize.Width) / 2;
  23. var top = GetTop(internalChild);
  24. if (!double.IsNaN(top))
  25. {
  26. y = top;
  27. }
  28. else
  29. {
  30. var bottom = GetBottom(internalChild);
  31. if (!double.IsNaN(bottom))
  32. y = arrangeSize.Height - internalChild.DesiredSize.Height - bottom;
  33. }
  34. }
  35. else
  36. {
  37. y = (arrangeSize.Height - internalChild.DesiredSize.Height) / 2;
  38. var left = GetLeft(internalChild);
  39. if (!double.IsNaN(left))
  40. {
  41. x = left;
  42. }
  43. else
  44. {
  45. var right = GetRight(internalChild);
  46. if (!double.IsNaN(right))
  47. x = arrangeSize.Width - internalChild.DesiredSize.Width - right;
  48. }
  49. }
  50. internalChild.Arrange(new Rect(new Point(x, y), internalChild.DesiredSize));
  51. }
  52. return arrangeSize;
  53. }
  54. }