diff --git a/components/Primitives/samples/WrapPanel.md b/components/Primitives/samples/WrapPanel.md index 8a85ebbe..a37d3dad 100644 --- a/components/Primitives/samples/WrapPanel.md +++ b/components/Primitives/samples/WrapPanel.md @@ -20,6 +20,9 @@ Spacing can be automatically added between items using the HorizontalSpacing and When the Orientation is Vertical, HorizontalSpacing adds uniform spacing between each column of items, and VerticalSpacing adds uniform vertical spacing between individual items. +> [!NOTE] +> When `StretchChild="Last"` is set, the last child will only stretch if the available measure size is finite. If the panel is measured with an infinite width (for horizontal orientation) or infinite height (for vertical orientation), the last child will not stretch. + > [!SAMPLE WrapPanelSample] ## Examples diff --git a/components/Primitives/src/WrapPanel/WrapPanel.cs b/components/Primitives/src/WrapPanel/WrapPanel.cs index 58561458..80bbd96f 100644 --- a/components/Primitives/src/WrapPanel/WrapPanel.cs +++ b/components/Primitives/src/WrapPanel/WrapPanel.cs @@ -97,6 +97,12 @@ public Thickness Padding /// /// Gets or sets a value indicating how to arrange child items /// + /// + /// When the available size provided to the panel is infinite (for example, + /// when placed in a container with Auto sizing), the last child will not be + /// stretched. Attempting to stretch in this scenario would cause the element + /// to expand to an infinite size and result in a runtime exception. + /// public StretchChild StretchChild { get { return (StretchChild)GetValue(StretchChildProperty); } @@ -219,7 +225,8 @@ void Arrange(UIElement child, bool isLast = false) } // Stretch the last item to fill the available space - if (isLast) + // if the parent measure is not infinite + if (isLast && !double.IsInfinity(parentMeasure.U)) { desiredMeasure.U = parentMeasure.U - position.U; } diff --git a/components/Primitives/tests/Primitives.Tests.projitems b/components/Primitives/tests/Primitives.Tests.projitems index 39e21d6d..30ed7ac4 100644 --- a/components/Primitives/tests/Primitives.Tests.projitems +++ b/components/Primitives/tests/Primitives.Tests.projitems @@ -33,10 +33,23 @@ + AutoLayoutFixedElementZeroZeroSpecialPage.xaml + + HorizontalWrapPanelInsideParentWithInfinityWidth.xaml + + + HorizontalWrapPanelInsideParentWithLimitedWidth.xaml + + + VerticalWrapPanelInsideParentWithInfinityHeight.xaml + + + VerticalWrapPanelInsideParentWithLimitedHeight.xaml + @@ -47,6 +60,22 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + diff --git a/components/Primitives/tests/Test_WrapPanel_StretchChild.cs b/components/Primitives/tests/Test_WrapPanel_StretchChild.cs new file mode 100644 index 00000000..b2ac97a0 --- /dev/null +++ b/components/Primitives/tests/Test_WrapPanel_StretchChild.cs @@ -0,0 +1,104 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using CommunityToolkit.Tests; +using CommunityToolkit.Tooling.TestGen; +using CommunityToolkit.WinUI.Controls; + +namespace PrimitivesTests; + +[TestClass] +public partial class Test_WrapPanel_StretchChild : VisualUITestBase +{ + /// + /// When a WrapPanel is inside a parent with infinite width, the last child cannot stretch to fill the remaining space. + /// Instead, it should measure to its desired size. + /// + [TestCategory("WrapPanel")] + [UIThreadTestMethod] + public void VerticalWrapPanelInsideParentWithInfinityHeightTest(VerticalWrapPanelInsideParentWithInfinityHeight page) + { + var wrapPanel = page.FindDescendant(); + Assert.IsNotNull(wrapPanel, "Could not find WrapPanel."); + Assert.IsFalse(wrapPanel.StretchChild is not StretchChild.Last, "WrapPanel StretchChild property not set to Last."); + Assert.IsFalse(wrapPanel.Children.Count < 1, "No children to test."); + + foreach (var child in wrapPanel.Children.Cast()) + { + double expectedHeight = child.DesiredSize.Height; + Assert.AreEqual(expectedHeight, child.ActualHeight, "Child height not as expected."); + } + } + + /// + /// When a WrapPanel is inside a parent with limited height, the last child with Stretch alignment should fill the remaining space. + /// + [TestCategory("WrapPanel")] + [UIThreadTestMethod] + public void VerticalWrapPanelInsideParentWithLimitedHeightTest(VerticalWrapPanelInsideParentWithLimitedHeight page) + { + var wrapPanel = page.FindDescendant(); + Assert.IsNotNull(wrapPanel, "Could not find WrapPanel."); + Assert.IsFalse(wrapPanel.StretchChild is not StretchChild.Last, "WrapPanel StretchChild property not set to Last."); + Assert.IsFalse(wrapPanel.Children.Count < 1, "No children to test."); + + var precedingChildren = wrapPanel.Children.Cast().Take(wrapPanel.Children.Count - 1); + + foreach (var child in precedingChildren) + { + double expectedHeight = child.DesiredSize.Height; + Assert.AreEqual(expectedHeight, child.ActualHeight, "Preceding child height not as expected."); + } + + var lastChild = wrapPanel.Children.Cast().Last(); + double lastChildExpectedHeight = wrapPanel.ActualHeight - precedingChildren.Sum(child => child.ActualHeight); + Assert.AreEqual(lastChildExpectedHeight, lastChild.ActualHeight, "Last child height not as expected."); + } + + /// + /// When a WrapPanel is inside a parent with infinite width, the last child cannot stretch to fill the remaining space. + /// Instead, it should measure to its desired size. + /// + [TestCategory("WrapPanel")] + [UIThreadTestMethod] + public void HorizontalWrapPanelInsideParentWithInfinityWidthTest(HorizontalWrapPanelInsideParentWithInfinityWidth page) + { + var wrapPanel = page.FindDescendant(); + Assert.IsNotNull(wrapPanel, "Could not find WrapPanel."); + Assert.IsFalse(wrapPanel.StretchChild is not StretchChild.Last, "WrapPanel StretchChild property not set to Last."); + Assert.IsFalse(wrapPanel.Children.Count < 1, "No children to test."); + + foreach (var child in wrapPanel.Children.Cast()) + { + double expectedWidth = child.DesiredSize.Width; + Assert.AreEqual(expectedWidth, child.ActualWidth, "Preceding child width not as expected."); + } + } + + /// + /// When a WrapPanel is inside a parent with limited width, the last child with Stretch alignment should fill the remaining space. + /// + /// + [TestCategory("WrapPanel")] + [UIThreadTestMethod] + public void HorizontalWrapPanelInsideParentWithLimitedWidthTest(HorizontalWrapPanelInsideParentWithLimitedWidth page) + { + var wrapPanel = page.FindDescendant(); + Assert.IsNotNull(wrapPanel, "Could not find WrapPanel."); + Assert.IsFalse(wrapPanel.StretchChild is not StretchChild.Last, "WrapPanel StretchChild property not set to Last."); + Assert.IsFalse(wrapPanel.Children.Count < 1, "No children to test."); + + var precedingChildren = wrapPanel.Children.Cast().Take(wrapPanel.Children.Count - 1); + + foreach (var child in precedingChildren) + { + double expectedWidth = child.DesiredSize.Width; + Assert.AreEqual(expectedWidth, child.ActualWidth, "Child width not as expected."); + } + + var lastChild = wrapPanel.Children.Cast().Last(); + double lastChildExpectedWidth = wrapPanel.ActualWidth - precedingChildren.Sum(child => child.ActualWidth); + Assert.AreEqual(lastChildExpectedWidth, lastChild.ActualWidth, "Last child width not as expected."); + } +} diff --git a/components/Primitives/tests/WrapPanel/HorizontalWrapPanelInsideParentWithInfinityWidth.xaml b/components/Primitives/tests/WrapPanel/HorizontalWrapPanelInsideParentWithInfinityWidth.xaml new file mode 100644 index 00000000..8f4d1f52 --- /dev/null +++ b/components/Primitives/tests/WrapPanel/HorizontalWrapPanelInsideParentWithInfinityWidth.xaml @@ -0,0 +1,26 @@ + + + + + + + +