SimpleStackPanel.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. namespace HandyControl.Controls;
  5. public class SimpleStackPanel : Panel
  6. {
  7. public static readonly DependencyProperty OrientationProperty =
  8. StackPanel.OrientationProperty.AddOwner(typeof(SimpleStackPanel),
  9. new FrameworkPropertyMetadata(Orientation.Vertical, FrameworkPropertyMetadataOptions.AffectsMeasure));
  10. public Orientation Orientation
  11. {
  12. get => (Orientation) GetValue(OrientationProperty);
  13. set => SetValue(OrientationProperty, value);
  14. }
  15. protected override Size MeasureOverride(Size constraint)
  16. {
  17. var stackDesiredSize = new Size();
  18. var children = InternalChildren;
  19. var layoutSlotSize = constraint;
  20. if (Orientation == Orientation.Horizontal)
  21. {
  22. layoutSlotSize.Width = double.PositiveInfinity;
  23. for (int i = 0, count = children.Count; i < count; ++i)
  24. {
  25. var child = children[i];
  26. if (child == null) continue;
  27. child.Measure(layoutSlotSize);
  28. var childDesiredSize = child.DesiredSize;
  29. stackDesiredSize.Width += childDesiredSize.Width;
  30. stackDesiredSize.Height = Math.Max(stackDesiredSize.Height, childDesiredSize.Height);
  31. }
  32. }
  33. else
  34. {
  35. layoutSlotSize.Height = double.PositiveInfinity;
  36. for (int i = 0, count = children.Count; i < count; ++i)
  37. {
  38. var child = children[i];
  39. if (child == null) continue;
  40. child.Measure(layoutSlotSize);
  41. var childDesiredSize = child.DesiredSize;
  42. stackDesiredSize.Width = Math.Max(stackDesiredSize.Width, childDesiredSize.Width);
  43. stackDesiredSize.Height += childDesiredSize.Height;
  44. }
  45. }
  46. return stackDesiredSize;
  47. }
  48. protected override Size ArrangeOverride(Size arrangeSize)
  49. {
  50. var children = InternalChildren;
  51. var rcChild = new Rect(arrangeSize);
  52. var previousChildSize = 0.0;
  53. if (Orientation == Orientation.Horizontal)
  54. {
  55. for (int i = 0, count = children.Count; i < count; ++i)
  56. {
  57. var child = children[i];
  58. if (child == null) continue;
  59. rcChild.X += previousChildSize;
  60. previousChildSize = child.DesiredSize.Width;
  61. rcChild.Width = previousChildSize;
  62. rcChild.Height = Math.Max(arrangeSize.Height, child.DesiredSize.Height);
  63. child.Arrange(rcChild);
  64. }
  65. }
  66. else
  67. {
  68. for (int i = 0, count = children.Count; i < count; ++i)
  69. {
  70. var child = children[i];
  71. if (child == null) continue;
  72. rcChild.Y += previousChildSize;
  73. previousChildSize = child.DesiredSize.Height;
  74. rcChild.Height = previousChildSize;
  75. rcChild.Width = Math.Max(arrangeSize.Width, child.DesiredSize.Width);
  76. child.Arrange(rcChild);
  77. }
  78. }
  79. return arrangeSize;
  80. }
  81. }