Skip to content

Commit 5e81f11

Browse files
Fix VerticalGrid column shrinking issue in Windows CollectionView
Co-authored-by: NanthiniMahalingam <[email protected]>
1 parent 10ebf92 commit 5e81f11

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

src/Controls/src/Core/Platform/Windows/CollectionView/FormsGridView.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ void UpdateItemSize()
115115

116116
if (_orientation == Orientation.Horizontal)
117117
{
118-
_wrapGrid.ItemHeight = Math.Floor(_wrapGrid.ActualHeight / Span);
118+
_wrapGrid.ItemHeight = Math.Round(_wrapGrid.ActualHeight / Span);
119119
}
120120
else
121121
{
122-
_wrapGrid.ItemWidth = Math.Floor(_wrapGrid.ActualWidth / Span);
122+
_wrapGrid.ItemWidth = Math.Round(_wrapGrid.ActualWidth / Span);
123123
}
124124
}
125125

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="Maui.Controls.Sample.Issues.IssueVerticalGridShrinking">
5+
6+
<Grid RowDefinitions="Auto,*">
7+
<Label Text="VerticalGrid items should fill available width when resized"
8+
AutomationId="InstructionLabel"
9+
Grid.Row="0"
10+
FontSize="16"
11+
Margin="10"/>
12+
13+
<CollectionView x:Name="collectionView"
14+
AutomationId="TestCollectionView"
15+
Grid.Row="1"
16+
Margin="10"
17+
BackgroundColor="LightGray"
18+
ItemsSource="{Binding Items}">
19+
20+
<CollectionView.ItemsLayout>
21+
<GridItemsLayout Orientation="Vertical"
22+
Span="2"
23+
HorizontalItemSpacing="5"
24+
VerticalItemSpacing="5"/>
25+
</CollectionView.ItemsLayout>
26+
27+
<CollectionView.ItemTemplate>
28+
<DataTemplate>
29+
<Grid BackgroundColor="LightBlue"
30+
Padding="10"
31+
AutomationId="ItemGrid">
32+
<Label Text="{Binding}"
33+
FontSize="14"
34+
HorizontalOptions="Center"
35+
VerticalOptions="Center"
36+
AutomationId="ItemLabel"/>
37+
</Grid>
38+
</DataTemplate>
39+
</CollectionView.ItemTemplate>
40+
</CollectionView>
41+
</Grid>
42+
43+
</ContentPage>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections.ObjectModel;
2+
3+
namespace Maui.Controls.Sample.Issues
4+
{
5+
[Issue(IssueTracker.Github, 17, "VerticalGrid column in CollectionView does not shrink in Windows", PlatformAffected.Windows)]
6+
public partial class IssueVerticalGridShrinking : ContentPage
7+
{
8+
public ObservableCollection<string> Items { get; set; }
9+
10+
public IssueVerticalGridShrinking()
11+
{
12+
InitializeComponent();
13+
14+
Items = new ObservableCollection<string>();
15+
for (int i = 1; i <= 20; i++)
16+
{
17+
Items.Add($"Item {i}");
18+
}
19+
20+
BindingContext = this;
21+
}
22+
}
23+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues
6+
{
7+
public class IssueVerticalGridShrinkingUITests : _IssuesUITest
8+
{
9+
public IssueVerticalGridShrinkingUITests(TestDevice device)
10+
: base(device)
11+
{
12+
}
13+
14+
public override string Issue => "VerticalGrid column in CollectionView does not shrink in Windows";
15+
16+
[Test]
17+
[Category(UITestCategories.CollectionView)]
18+
public void VerticalGridItemsShouldFillAvailableWidth()
19+
{
20+
// Wait for the CollectionView to load
21+
App.WaitForElement("TestCollectionView");
22+
23+
// Verify instruction label is present
24+
App.WaitForElement("InstructionLabel");
25+
26+
// Wait for at least one item to be loaded
27+
App.WaitForElement("ItemGrid");
28+
29+
// The test should verify that grid items fill the available width properly
30+
// Without the fix, items would be smaller due to Math.Floor calculation
31+
// With the fix, items should utilize available space better
32+
33+
// This is primarily a visual test - the fix ensures items don't appear cropped
34+
// or leave excessive empty space on the right when the container width
35+
// doesn't divide evenly by the span count
36+
37+
// For automated testing, we just verify the UI loads without exceptions
38+
var items = App.QueryElements("ItemGrid");
39+
Assert.That(items.Count, Is.GreaterThan(0), "CollectionView should contain grid items");
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)