Skip to content

Commit

Permalink
[Windows] Fix default item min height in CollectionView (#12811)
Browse files Browse the repository at this point in the history
* Fix default item min height in Windows CollectionView

* Fixed build error
  • Loading branch information
hartez committed Aug 30, 2023
1 parent 8938a80 commit ff7c988
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -22,7 +26,7 @@ public async Task CollectionViewHandlerDisconnects()

var collectionView = new CollectionView()
{
ItemTemplate = new DataTemplate(() =>
ItemTemplate = new Controls.DataTemplate(() =>
{
return new VerticalStackLayout()
{
Expand All @@ -49,5 +53,52 @@ await CreateHandlerAndAddToWindow<LayoutHandler>(layout, (handler) =>
return Task.CompletedTask;
});
}

[Fact]
public async Task ValidateItemContainerDefaultHeight()
{
SetupBuilder();
ObservableCollection<string> data = new ObservableCollection<string>()
{
"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<LayoutHandler>(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<WSetter>()
.FirstOrDefault(X => X.Property == FrameworkElement.MinHeightProperty).Value;

Assert.Equal(0d, minHeight);
}
}
}

0 comments on commit ff7c988

Please sign in to comment.