Fix for CollectionView with GridItemsLayout (Span=1) doesn't adapt to window width reduction on Windows platform#31038
Conversation
|
Hey there @@praveenkumarkarunanithi! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a window resizing issue with CollectionView using GridItemsLayout on Windows where single-column layouts (Span=1) don't properly adapt when the window width is reduced. The root cause is that setting ItemWidth to the full container width prevents the UWP SizeChanged event from firing during resize operations.
- Conditionally skips setting ItemWidth for single-column layouts (Span=1) to allow native Auto layout behavior
- Adds comprehensive UI test coverage to validate the fix behavior
- Maintains existing functionality for multi-column layouts (Span>1)
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/Controls/src/Core/Platform/Windows/CollectionView/FormsGridView.cs |
Core fix that conditionally clears ItemWidth for single-column GridItemsLayout |
src/Controls/tests/TestCases.HostApp/Issues/Issue23702.cs |
UI test page demonstrating the CollectionView resize behavior |
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue23702.cs |
Automated test validating that items resize correctly when width decreases |
Comments suppressed due to low confidence (1)
| label.PropertyChanged += (sender, e) => | ||
| { | ||
| if (e.PropertyName == "Width" && sender is Label sl && sl.Width > 0) | ||
| { | ||
| _widthLabel.Text = Math.Round(sl.Width).ToString(); | ||
| } | ||
| }; |
There was a problem hiding this comment.
PropertyChanged event handler is attached inside the DataTemplate factory method, which means a new handler is created for every item in the collection. This could lead to memory leaks if items are recycled. Consider using a WeakEventManager or detaching the handler when appropriate.
| label.PropertyChanged += (sender, e) => | |
| { | |
| if (e.PropertyName == "Width" && sender is Label sl && sl.Width > 0) | |
| { | |
| _widthLabel.Text = Math.Round(sl.Width).ToString(); | |
| } | |
| }; | |
| WeakEventManager<PropertyChangedEventArgs>.AddHandler(label, nameof(label.PropertyChanged), (sender, e) => | |
| { | |
| if (e.PropertyName == "Width" && sender is Label sl && sl.Width > 0) | |
| { | |
| _widthLabel.Text = Math.Round(sl.Width).ToString(); | |
| } | |
| }); |
|
/azp run MAUI-UITests-public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
|
||
| [Test] | ||
| [Category(UITestCategories.CollectionView)] | ||
| public void ItemsWrapGridShouldUpdateBasedOnCollectionViewSize() |
There was a problem hiding this comment.
@jsuarezruiz updated the images for UI test from CI.
|
/azp run MAUI-UITests-public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
Are there any chances that this error will be fixed in maui 9? I've been waiting for a long time... |
… window width reduction on Windows platform (#31038) * fix update. * updated test case * optimised test case * Update FormsGridView.cs * updated test case for 26083 * updated UI test images from CI * updated UI test from CI-2
… window width reduction on Windows platform (#31038) * fix update. * updated test case * optimised test case * Update FormsGridView.cs * updated test case for 26083 * updated UI test images from CI * updated UI test from CI-2
… window width reduction on Windows platform (#31038) * fix update. * updated test case * optimised test case * Update FormsGridView.cs * updated test case for 26083 * updated UI test images from CI * updated UI test from CI-2
… window width reduction on Windows platform (#31038) * fix update. * updated test case * optimised test case * Update FormsGridView.cs * updated test case for 26083 * updated UI test images from CI * updated UI test from CI-2
… window width reduction on Windows platform (#31038) * fix update. * updated test case * optimised test case * Update FormsGridView.cs * updated test case for 26083 * updated UI test images from CI * updated UI test from CI-2
… window width reduction on Windows platform (#31038) * fix update. * updated test case * optimised test case * Update FormsGridView.cs * updated test case for 26083 * updated UI test images from CI * updated UI test from CI-2
… window width reduction on Windows platform (#31038) * fix update. * updated test case * optimised test case * Update FormsGridView.cs * updated test case for 26083 * updated UI test images from CI * updated UI test from CI-2
… window width reduction on Windows platform (#31038) * fix update. * updated test case * optimised test case * Update FormsGridView.cs * updated test case for 26083 * updated UI test images from CI * updated UI test from CI-2
… window width reduction on Windows platform (#31038) * fix update. * updated test case * optimised test case * Update FormsGridView.cs * updated test case for 26083 * updated UI test images from CI * updated UI test from CI-2
… window width reduction on Windows platform (#31038) * fix update. * updated test case * optimised test case * Update FormsGridView.cs * updated test case for 26083 * updated UI test images from CI * updated UI test from CI-2

Root Cause
In the single-column case (
Span = 1), setting the ItemWidth on the native UWPItemsWrapGridto match the full container width causes the layout system to treat the item size as fixed. As a result, no size change is detected during window resizing, and theSizeChangedevent does not fire. In contrast, whenSpan > 1, the ItemWidth is smaller than the container, allowing the layout to reflow and correctly triggerSizeChanged.This behavior aligns with UWP documentation, which states that
SizeChangedoccurs only whenActualWidthorActualHeightchanges, and does not fire “if the position of the object within a parent container changes, but not the size.”Description of Change
The fix updates the
UpdateItemSizemethod inFormsGridView.csto conditionally skip setting theItemWidthwhen the layout has only one column (Span == 1). In this case, ItemWidth remains at its default value, allowing the native UWPItemsWrapGridto use its built-in Auto layout behavior. This ensures that the control remains responsive and correctly handles window size changes.Issues Fixed
Fixes #23702
#26083
Tested the behaviour in the following platforms
Screenshots