From 702a54165d2c5fad6fd66ab164b9cc44b39ffd48 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Wed, 30 Aug 2023 04:44:43 -0600 Subject: [PATCH] [Windows] Fix default item min height in CollectionView (#12811) (#17092) * Fix default item min height in Windows CollectionView * Fixed build error --- .../StructuredItemsViewHandler.Windows.cs | 12 ++-- .../CollectionViewTests.Windows.cs | 55 ++++++++++++++++++- 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/src/Controls/src/Core/Handlers/Items/StructuredItemsViewHandler.Windows.cs b/src/Controls/src/Core/Handlers/Items/StructuredItemsViewHandler.Windows.cs index 5e3fb585ad2c..fefd6a0282ac 100644 --- a/src/Controls/src/Core/Handlers/Items/StructuredItemsViewHandler.Windows.cs +++ b/src/Controls/src/Core/Handlers/Items/StructuredItemsViewHandler.Windows.cs @@ -221,8 +221,8 @@ static WStyle GetItemContainerStyle(GridItemsLayout layout) var style = new WStyle(typeof(GridViewItem)); - style.Setters.Add(new WSetter(GridViewItem.MarginProperty, margin)); - style.Setters.Add(new WSetter(GridViewItem.PaddingProperty, WinUIHelpers.CreateThickness(0))); + style.Setters.Add(new WSetter(FrameworkElement.MarginProperty, margin)); + style.Setters.Add(new WSetter(Control.PaddingProperty, WinUIHelpers.CreateThickness(0))); style.Setters.Add(new WSetter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Stretch)); return style; @@ -235,8 +235,9 @@ static WStyle GetVerticalItemContainerStyle(LinearItemsLayout layout) var style = new WStyle(typeof(ListViewItem)); - style.Setters.Add(new WSetter(ListViewItem.MarginProperty, margin)); - style.Setters.Add(new WSetter(GridViewItem.PaddingProperty, WinUIHelpers.CreateThickness(0))); + style.Setters.Add(new WSetter(FrameworkElement.MinHeightProperty, 0)); + style.Setters.Add(new WSetter(FrameworkElement.MarginProperty, margin)); + style.Setters.Add(new WSetter(Control.PaddingProperty, WinUIHelpers.CreateThickness(0))); style.Setters.Add(new WSetter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Stretch)); return style; @@ -249,7 +250,8 @@ static WStyle GetHorizontalItemContainerStyle(LinearItemsLayout layout) var style = new WStyle(typeof(ListViewItem)); - style.Setters.Add(new WSetter(ListViewItem.PaddingProperty, padding)); + style.Setters.Add(new WSetter(FrameworkElement.MinWidthProperty, 0)); + style.Setters.Add(new WSetter(Control.PaddingProperty, padding)); style.Setters.Add(new WSetter(Control.VerticalContentAlignmentProperty, VerticalAlignment.Stretch)); return style; diff --git a/src/Controls/tests/DeviceTests/Elements/CollectionView/CollectionViewTests.Windows.cs b/src/Controls/tests/DeviceTests/Elements/CollectionView/CollectionViewTests.Windows.cs index d171003ab068..4219532c86f4 100644 --- a/src/Controls/tests/DeviceTests/Elements/CollectionView/CollectionViewTests.Windows.cs +++ b/src/Controls/tests/DeviceTests/Elements/CollectionView/CollectionViewTests.Windows.cs @@ -1,8 +1,12 @@ -using System.Collections.ObjectModel; +using System.Linq; +using System.Collections.ObjectModel; using System.Threading.Tasks; using Microsoft.Maui.Controls; using Microsoft.Maui.Controls.Handlers.Items; using Microsoft.Maui.Handlers; +using Xunit; +using Microsoft.UI.Xaml; +using WSetter = Microsoft.UI.Xaml.Setter; namespace Microsoft.Maui.DeviceTests { @@ -22,7 +26,7 @@ public async Task CollectionViewHandlerDisconnects() var collectionView = new CollectionView() { - ItemTemplate = new DataTemplate(() => + ItemTemplate = new Controls.DataTemplate(() => { return new VerticalStackLayout() { @@ -49,5 +53,52 @@ await CreateHandlerAndAddToWindow(layout, (handler) => return Task.CompletedTask; }); } + + [Fact] + public async Task ValidateItemContainerDefaultHeight() + { + SetupBuilder(); + ObservableCollection data = new ObservableCollection() + { + "Item 1", + "Item 2", + "Item 3" + }; + + var collectionView = new CollectionView() + { + ItemTemplate = new Controls.DataTemplate(() => + { + return new VerticalStackLayout() + { + new Label() + }; + }), + ItemsSource = data + }; + + var layout = new VerticalStackLayout() + { + collectionView + }; + + await CreateHandlerAndAddToWindow(layout, async (handler) => + { + await Task.Delay(100); + ValidateItemContainerStyle(collectionView); + }); + } + + void ValidateItemContainerStyle(CollectionView collectionView) + { + var handler = (CollectionViewHandler)collectionView.Handler; + var control = handler.PlatformView; + + var minHeight = control.ItemContainerStyle.Setters + .OfType() + .FirstOrDefault(X => X.Property == FrameworkElement.MinHeightProperty).Value; + + Assert.Equal(0d, minHeight); + } } } \ No newline at end of file