SimplePanel.cs 1015 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. namespace HandyControl.Controls;
  5. /// <summary>
  6. /// 用以代替Grid
  7. /// </summary>
  8. /// <remarks>
  9. /// 当不需要Grid的行、列分隔等功能时建议用此轻量级类代替
  10. /// </remarks>
  11. public class SimplePanel : Panel
  12. {
  13. protected override Size MeasureOverride(Size constraint)
  14. {
  15. var maxSize = new Size();
  16. foreach (UIElement child in InternalChildren)
  17. {
  18. if (child != null)
  19. {
  20. child.Measure(constraint);
  21. maxSize.Width = Math.Max(maxSize.Width, child.DesiredSize.Width);
  22. maxSize.Height = Math.Max(maxSize.Height, child.DesiredSize.Height);
  23. }
  24. }
  25. return maxSize;
  26. }
  27. protected override Size ArrangeOverride(Size arrangeSize)
  28. {
  29. foreach (UIElement child in InternalChildren)
  30. {
  31. child?.Arrange(new Rect(arrangeSize));
  32. }
  33. return arrangeSize;
  34. }
  35. }