123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- using System;
- using System.Windows;
- using System.Windows.Controls;
- namespace HandyControl.Controls;
- public class SimpleStackPanel : Panel
- {
- public static readonly DependencyProperty OrientationProperty =
- StackPanel.OrientationProperty.AddOwner(typeof(SimpleStackPanel),
- new FrameworkPropertyMetadata(Orientation.Vertical, FrameworkPropertyMetadataOptions.AffectsMeasure));
- public Orientation Orientation
- {
- get => (Orientation) GetValue(OrientationProperty);
- set => SetValue(OrientationProperty, value);
- }
- protected override Size MeasureOverride(Size constraint)
- {
- var stackDesiredSize = new Size();
- var children = InternalChildren;
- var layoutSlotSize = constraint;
- if (Orientation == Orientation.Horizontal)
- {
- layoutSlotSize.Width = double.PositiveInfinity;
- for (int i = 0, count = children.Count; i < count; ++i)
- {
- var child = children[i];
- if (child == null) continue;
- child.Measure(layoutSlotSize);
- var childDesiredSize = child.DesiredSize;
- stackDesiredSize.Width += childDesiredSize.Width;
- stackDesiredSize.Height = Math.Max(stackDesiredSize.Height, childDesiredSize.Height);
- }
- }
- else
- {
- layoutSlotSize.Height = double.PositiveInfinity;
- for (int i = 0, count = children.Count; i < count; ++i)
- {
- var child = children[i];
- if (child == null) continue;
- child.Measure(layoutSlotSize);
- var childDesiredSize = child.DesiredSize;
- stackDesiredSize.Width = Math.Max(stackDesiredSize.Width, childDesiredSize.Width);
- stackDesiredSize.Height += childDesiredSize.Height;
- }
- }
- return stackDesiredSize;
- }
- protected override Size ArrangeOverride(Size arrangeSize)
- {
- var children = InternalChildren;
- var rcChild = new Rect(arrangeSize);
- var previousChildSize = 0.0;
- if (Orientation == Orientation.Horizontal)
- {
- for (int i = 0, count = children.Count; i < count; ++i)
- {
- var child = children[i];
- if (child == null) continue;
- rcChild.X += previousChildSize;
- previousChildSize = child.DesiredSize.Width;
- rcChild.Width = previousChildSize;
- rcChild.Height = Math.Max(arrangeSize.Height, child.DesiredSize.Height);
- child.Arrange(rcChild);
- }
- }
- else
- {
- for (int i = 0, count = children.Count; i < count; ++i)
- {
- var child = children[i];
- if (child == null) continue;
- rcChild.Y += previousChildSize;
- previousChildSize = child.DesiredSize.Height;
- rcChild.Height = previousChildSize;
- rcChild.Width = Math.Max(arrangeSize.Width, child.DesiredSize.Width);
- child.Arrange(rcChild);
- }
- }
- return arrangeSize;
- }
- }
|