From 8ec3ab1a5aeb7519aacf5f8716b183adf6496260 Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Wed, 1 Sep 2021 22:30:26 +0800 Subject: [PATCH] optimized UniformSpacingPanel --- .../Controls/Panel/UniformSpacingPanel.cs | 54 ++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/src/Shared/HandyControl_Shared/Controls/Panel/UniformSpacingPanel.cs b/src/Shared/HandyControl_Shared/Controls/Panel/UniformSpacingPanel.cs index 219788ba..82eeda9f 100644 --- a/src/Shared/HandyControl_Shared/Controls/Panel/UniformSpacingPanel.cs +++ b/src/Shared/HandyControl_Shared/Controls/Panel/UniformSpacingPanel.cs @@ -84,6 +84,26 @@ namespace HandyControl.Controls return double.IsNaN(v) || v >= 0.0d && !double.IsPositiveInfinity(v); } + public static readonly DependencyProperty ItemHorizontalAlignmentProperty = DependencyProperty.Register( + "ItemHorizontalAlignment", typeof(HorizontalAlignment?), typeof(UniformSpacingPanel), + new FrameworkPropertyMetadata(HorizontalAlignment.Stretch, FrameworkPropertyMetadataOptions.AffectsMeasure)); + + public HorizontalAlignment? ItemHorizontalAlignment + { + get => (HorizontalAlignment?) GetValue(ItemHorizontalAlignmentProperty); + set => SetValue(ItemHorizontalAlignmentProperty, value); + } + + public static readonly DependencyProperty ItemVerticalAlignmentProperty = DependencyProperty.Register( + "ItemVerticalAlignment", typeof(VerticalAlignment?), typeof(UniformSpacingPanel), + new FrameworkPropertyMetadata(VerticalAlignment.Stretch, FrameworkPropertyMetadataOptions.AffectsMeasure)); + + public VerticalAlignment? ItemVerticalAlignment + { + get => (VerticalAlignment?) GetValue(ItemVerticalAlignmentProperty); + set => SetValue(ItemVerticalAlignmentProperty, value); + } + private void ArrangeWrapLine(double v, double lineV, int start, int end, bool useItemU, double itemU, double spacing) { double u = 0; @@ -100,7 +120,10 @@ namespace HandyControl.Controls child.Arrange(isHorizontal ? new Rect(u, v, layoutSlotU, lineV) : new Rect(v, u, lineV, layoutSlotU)); - u += layoutSlotU + spacing; + if (layoutSlotU > 0) + { + u += layoutSlotU + spacing; + } } } @@ -120,7 +143,10 @@ namespace HandyControl.Controls child.Arrange(isHorizontal ? new Rect(u, 0, layoutSlotU, lineV) : new Rect(0, u, lineV, layoutSlotU)); - u += layoutSlotU + spacing; + if (layoutSlotU > 0) + { + u += layoutSlotU + spacing; + } } } @@ -135,6 +161,10 @@ namespace HandyControl.Controls var itemHeightSet = !double.IsNaN(itemHeight); var spacing = Spacing; var childWrapping = ChildWrapping; + var itemHorizontalAlignment = ItemHorizontalAlignment; + var itemVerticalAlignment = ItemVerticalAlignment; + var itemHorizontalAlignmentSet = itemHorizontalAlignment != null; + var itemVerticalAlignmentSet = ItemVerticalAlignment != null; var childConstraint = new Size( itemWidthSet ? itemWidth : constraint.Width, @@ -150,6 +180,16 @@ namespace HandyControl.Controls var child = children[i]; if (child == null) continue; + if (itemHorizontalAlignmentSet) + { + child.SetCurrentValue(HorizontalAlignmentProperty, itemHorizontalAlignment); + } + + if (itemVerticalAlignmentSet) + { + child.SetCurrentValue(VerticalAlignmentProperty, itemVerticalAlignment); + } + child.Measure(childConstraint); var sz = new UVSize( @@ -199,6 +239,16 @@ namespace HandyControl.Controls var child = children[i]; if (child == null) continue; + if (itemHorizontalAlignmentSet) + { + child.SetCurrentValue(HorizontalAlignmentProperty, itemHorizontalAlignment); + } + + if (itemVerticalAlignmentSet) + { + child.SetCurrentValue(VerticalAlignmentProperty, itemVerticalAlignment); + } + child.Measure(layoutSlotSize); var sz = new UVSize(