Skip to content

Commit a4ef877

Browse files
Fixed CollectionView does not update layout correctly when ItemsSource changes (#30978)
* Fixed CollectionView does not update layout correctly when ItemsSource changes * Optimized fix
1 parent 44236a8 commit a4ef877

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

src/Controls/src/Core/Handlers/Items2/ItemsViewHandler2.iOS.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ public override Size GetDesiredSize(double widthConstraint, double heightConstra
184184
{
185185
var contentSize = Controller.GetSize();
186186

187+
// If contentSize comes back null, it means none of the content has been realized yet;
188+
// we need to return the expansive size the collection view wants by default to get
189+
// it to start measuring its content
190+
if (contentSize.Height == 0 || contentSize.Width == 0)
191+
{
192+
return base.GetDesiredSize(widthConstraint, heightConstraint);
193+
}
194+
187195
// Our target size is the smaller of it and the constraints
188196
var width = contentSize.Width <= widthConstraint ? contentSize.Width : widthConstraint;
189197
var height = contentSize.Height <= heightConstraint ? contentSize.Height : heightConstraint;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System.Collections.ObjectModel;
2+
3+
namespace Maui.Controls.Sample.Issues;
4+
5+
[Issue(IssueTracker.Github, 30953, "CollectionView does not update layout correctly when ItemsSource changes", PlatformAffected.iOS | PlatformAffected.macOS)]
6+
public class Issue30953 : ContentPage
7+
{
8+
private ObservableCollection<string> Countries = new();
9+
10+
public Issue30953()
11+
{
12+
LoadCountries();
13+
var collectionView = new CollectionView2();
14+
var button = new Button
15+
{
16+
Text = "Set ItemsSource",
17+
AutomationId = "Issue30953Button",
18+
HorizontalOptions = LayoutOptions.Center
19+
};
20+
21+
button.Clicked += (sender, args) =>
22+
{
23+
collectionView.ItemsSource = Countries;
24+
};
25+
26+
var label = new Label
27+
{
28+
Text = "The test passed if the CollectionView ItemsSource is set and the items are displayed correctly in runtime.",
29+
HorizontalOptions = LayoutOptions.Center
30+
};
31+
32+
var stack = new VerticalStackLayout
33+
{
34+
Children = {
35+
label,
36+
button,
37+
collectionView
38+
}
39+
};
40+
41+
Content = stack;
42+
}
43+
44+
void LoadCountries()
45+
{
46+
Countries = new ObservableCollection<string>
47+
{
48+
"United States",
49+
"Canada",
50+
"United Kingdom",
51+
"Germany",
52+
"France",
53+
"Italy",
54+
"Spain",
55+
"Japan",
56+
"Australia",
57+
"Brazil",
58+
"India",
59+
"China",
60+
"Russia",
61+
"Mexico",
62+
"Argentina",
63+
"South Africa",
64+
"Egypt",
65+
"Turkey",
66+
"Netherlands",
67+
"Sweden"
68+
};
69+
}
70+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue30953 : _IssuesUITest
8+
{
9+
public Issue30953(TestDevice testDevice) : base(testDevice)
10+
{
11+
}
12+
public override string Issue => "CollectionView does not update layout correctly when ItemsSource changes";
13+
14+
[Test]
15+
[Category(UITestCategories.CollectionView)]
16+
public void EnsureCollectionViewLayoutOnItemsSourceChange()
17+
{
18+
App.WaitForElement("Issue30953Button");
19+
App.Tap("Issue30953Button");
20+
App.WaitForElement("United States");
21+
}
22+
}

0 commit comments

Comments
 (0)