Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Fix default item min height in CollectionView #12811

Merged
merged 3 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,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 @@ -208,8 +208,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 @@ -222,7 +223,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);
}
}
}