Skip to content
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 @@ -184,6 +184,14 @@ public override Size GetDesiredSize(double widthConstraint, double heightConstra
{
var contentSize = Controller.GetSize();

// If contentSize comes back null, it means none of the content has been realized yet;
// we need to return the expansive size the collection view wants by default to get
// it to start measuring its content
if (contentSize.Height == 0 || contentSize.Width == 0)
{
return base.GetDesiredSize(widthConstraint, heightConstraint);
}

// Our target size is the smaller of it and the constraints
var width = contentSize.Width <= widthConstraint ? contentSize.Width : widthConstraint;
var height = contentSize.Height <= heightConstraint ? contentSize.Height : heightConstraint;
Expand Down
70 changes: 70 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue30953.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Collections.ObjectModel;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 30953, "CollectionView does not update layout correctly when ItemsSource changes", PlatformAffected.iOS | PlatformAffected.macOS)]
public class Issue30953 : ContentPage
{
private ObservableCollection<string> Countries = new();

public Issue30953()
{
LoadCountries();
var collectionView = new CollectionView2();
var button = new Button
{
Text = "Set ItemsSource",
AutomationId = "Issue30953Button",
HorizontalOptions = LayoutOptions.Center
};

button.Clicked += (sender, args) =>
{
collectionView.ItemsSource = Countries;
};

var label = new Label
{
Text = "The test passed if the CollectionView ItemsSource is set and the items are displayed correctly in runtime.",
HorizontalOptions = LayoutOptions.Center
};

var stack = new VerticalStackLayout
{
Children = {
label,
button,
collectionView
}
};

Content = stack;
}

void LoadCountries()
{
Countries = new ObservableCollection<string>
{
"United States",
"Canada",
"United Kingdom",
"Germany",
"France",
"Italy",
"Spain",
"Japan",
"Australia",
"Brazil",
"India",
"China",
"Russia",
"Mexico",
"Argentina",
"South Africa",
"Egypt",
"Turkey",
"Netherlands",
"Sweden"
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue30953 : _IssuesUITest
{
public Issue30953(TestDevice testDevice) : base(testDevice)
{
}
public override string Issue => "CollectionView does not update layout correctly when ItemsSource changes";

[Test]
[Category(UITestCategories.CollectionView)]
public void EnsureCollectionViewLayoutOnItemsSourceChange()
{
App.WaitForElement("Issue30953Button");
App.Tap("Issue30953Button");
App.WaitForElement("United States");
}
}
Loading