-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[iOS/Mac] CollectionView: Fix incorrect ItemsViewScrolledEventArgs indices with grouped items #34240
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
Merged
kubaflo
merged 2 commits into
dotnet:inflight/current
from
SyedAbdulAzeemSF4852:fix-17664_ios
Mar 14, 2026
+154
−2
Merged
[iOS/Mac] CollectionView: Fix incorrect ItemsViewScrolledEventArgs indices with grouped items #34240
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/Controls/tests/TestCases.HostApp/Issues/Issue17664.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| using System.Collections.ObjectModel; | ||
|
|
||
| namespace Maui.Controls.Sample.Issues; | ||
|
|
||
| [Issue(IssueTracker.Github, 17664, "Incorrect ItemsViewScrolledEventArgs in CollectionView when IsGrouped is set to true", PlatformAffected.iOS)] | ||
| public class Issue17664 : ContentPage | ||
| { | ||
| CollectionView _collectionView; | ||
| Label descriptionLabel; | ||
| ObservableCollection<Issue17664_ItemModelGroup> _groupedItems; | ||
|
|
||
| public Issue17664() | ||
| { | ||
| Button scrollButton = new Button | ||
| { | ||
| AutomationId = "Issue17664ScrollBtn", | ||
| Text = "Scroll to Category C, Item #2" | ||
| }; | ||
| scrollButton.Clicked += ScrollButton_Clicked; | ||
|
|
||
| descriptionLabel = new Label | ||
| { | ||
| AutomationId = "Issue17664DescriptionLabel", | ||
| Text = "Use the button above to scroll the CollectionView.", | ||
| FontSize = 14, | ||
| HorizontalOptions = LayoutOptions.Center | ||
| }; | ||
|
|
||
| _collectionView = new CollectionView | ||
| { | ||
| IsGrouped = true, | ||
| GroupHeaderTemplate = new DataTemplate(() => | ||
| { | ||
| Label label = new Label | ||
| { | ||
| FontAttributes = FontAttributes.Bold, | ||
| BackgroundColor = Colors.LightGray, | ||
| Padding = 10 | ||
| }; | ||
|
|
||
| label.SetBinding(Label.TextProperty, "Name"); | ||
| return label; | ||
| }), | ||
| ItemTemplate = new DataTemplate(() => | ||
| { | ||
| Label textLabel = new Label | ||
| { | ||
| FontAttributes = FontAttributes.Bold, | ||
| Padding = 30 | ||
| }; | ||
|
|
||
| textLabel.SetBinding(Label.TextProperty, "."); | ||
| return textLabel; | ||
| }) | ||
| }; | ||
|
|
||
| _collectionView.Scrolled += (s, e) => | ||
| { | ||
| var flatItems = _groupedItems.SelectMany(group => group).ToList(); | ||
| if (e.LastVisibleItemIndex >= 0 && e.LastVisibleItemIndex < flatItems.Count) | ||
| { | ||
| descriptionLabel.Text = flatItems[e.LastVisibleItemIndex]; | ||
| } | ||
| }; | ||
SyedAbdulAzeemSF4852 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| List<string> categories = new List<string> { "Category A", "Category B", "Category C" }; | ||
|
|
||
| _groupedItems = new ObservableCollection<Issue17664_ItemModelGroup>(); | ||
|
|
||
| foreach (var category in categories) | ||
| { | ||
| List<string> items = new List<string>(); | ||
|
|
||
| for (int i = 0; i < 5; i++) | ||
| { | ||
| items.Add($"{category} item #{i}"); | ||
| } | ||
|
|
||
| _groupedItems.Add(new Issue17664_ItemModelGroup(category, items)); | ||
| } | ||
|
|
||
| _collectionView.ItemsSource = _groupedItems; | ||
|
|
||
| Grid grid = new Grid | ||
| { | ||
| RowSpacing = 10, | ||
| Padding = 10, | ||
| RowDefinitions = | ||
| { | ||
| new RowDefinition { Height = GridLength.Auto }, | ||
| new RowDefinition { Height = GridLength.Auto }, | ||
| new RowDefinition { Height = GridLength.Star } | ||
| } | ||
| }; | ||
|
|
||
| grid.Add(scrollButton, 0, 0); | ||
| grid.Add(descriptionLabel, 0, 1); | ||
| grid.Add(_collectionView, 0, 2); | ||
|
|
||
| Content = grid; | ||
| } | ||
|
|
||
| private void ScrollButton_Clicked(object sender, EventArgs e) | ||
| { | ||
| var targetGroup = _groupedItems.FirstOrDefault(group => group.Name == "Category C"); | ||
| var targetItem = targetGroup.FirstOrDefault(item => item == "Category C item #2"); | ||
|
|
||
| _collectionView.ScrollTo(targetItem, targetGroup, ScrollToPosition.End); | ||
| } | ||
| } | ||
|
|
||
| public class Issue17664_ItemModelGroup : ObservableCollection<string> | ||
| { | ||
| public string Name { get; set; } | ||
|
|
||
| public Issue17664_ItemModelGroup(string name, IEnumerable<string> items) : base(items) | ||
| { | ||
| Name = name; | ||
| } | ||
| } | ||
30 changes: 30 additions & 0 deletions
30
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue17664.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #if TEST_FAILS_ON_ANDROID && TEST_FAILS_ON_WINDOWS // Android fix: https://github.com/dotnet/maui/pull/31437 | ||
| // Windows: The Scrolled event is not consistently triggered in the CI environment during automated | ||
| // scrolling, so the label text is never updated. This is a test infrastructure limitation on Windows; | ||
| // the fix itself is iOS/MacCatalyst-only and works correctly on iOS and MacCatalyst. | ||
| using NUnit.Framework; | ||
| using UITest.Appium; | ||
| using UITest.Core; | ||
|
|
||
| namespace Microsoft.Maui.TestCases.Tests.Issues; | ||
|
|
||
| public class Issue17664 : _IssuesUITest | ||
| { | ||
| public Issue17664(TestDevice device) : base(device) | ||
| { | ||
| } | ||
|
|
||
| public override string Issue => "Incorrect ItemsViewScrolledEventArgs in CollectionView when IsGrouped is set to true"; | ||
|
|
||
| [Test] | ||
| [Category(UITestCategories.CollectionView)] | ||
| public void VerifyGroupedCollectionViewVisibleItemIndices() | ||
| { | ||
| App.WaitForElement("Issue17664ScrollBtn"); | ||
| App.Tap("Issue17664ScrollBtn"); | ||
|
|
||
| var resultItem = App.WaitForElement("Issue17664DescriptionLabel").GetText(); | ||
| Assert.That(resultItem, Is.EqualTo("Category C item #2")); | ||
| } | ||
| } | ||
| #endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.