diff --git a/src/Controls/src/Core/Handlers/Items/iOS/ObservableGroupedSource.cs b/src/Controls/src/Core/Handlers/Items/iOS/ObservableGroupedSource.cs index 4111016b918a..3a26a708245e 100644 --- a/src/Controls/src/Core/Handlers/Items/iOS/ObservableGroupedSource.cs +++ b/src/Controls/src/Core/Handlers/Items/iOS/ObservableGroupedSource.cs @@ -378,6 +378,8 @@ int IndexInGroup(object item, object group) { return index; } + + index++; } return -1; } diff --git a/src/Controls/tests/TestCases.Android.Tests/snapshots/android/SelectionShouldNotMovedToTopWithGroupedCollection.png b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/SelectionShouldNotMovedToTopWithGroupedCollection.png new file mode 100644 index 000000000000..d9fb5af378d1 Binary files /dev/null and b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/SelectionShouldNotMovedToTopWithGroupedCollection.png differ diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue22320.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue22320.cs new file mode 100644 index 000000000000..a4397df40805 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue22320.cs @@ -0,0 +1,87 @@ +using System.Collections; +using System.Collections.ObjectModel; + +namespace Maui.Controls.Sample.Issues; +[Issue(IssueTracker.Github, 22320, "CollectionView does not highlight selected grouped items correctly", PlatformAffected.iOS | PlatformAffected.macOS)] +public class Issue22320 : ContentPage +{ + public Issue22320() + { + var layout = new StackLayout(); + var bindingContext = new Issue22320ViewModel(); + var collectionView = new CollectionView(); + collectionView.BindingContext = bindingContext; + collectionView.IsGrouped = true; + collectionView.AutomationId = "CollectionView"; + collectionView.SelectionMode = SelectionMode.Single; + + collectionView.ItemTemplate = new DataTemplate(() => + { + var border = new Border + { + HeightRequest = 20 + }; + + var label = new Label(); + label.SetBinding(Label.TextProperty, "."); + + border.Content = label; + + return border; + }); + + collectionView.GroupHeaderTemplate = new DataTemplate(() => + { + var border = new Border + { + Background = Colors.LightGrey, + HeightRequest = 20 + }; + + var label = new Label(); + label.SetBinding(Label.TextProperty, "Key"); + + border.Content = label; + + return border; + }); + + // Set the ItemsSource binding + collectionView.SetBinding(CollectionView.ItemsSourceProperty, "Items"); + layout.Children.Add(collectionView); + + Content = layout; + } +} + +public class Issue22320ItemGroup : IEnumerable +{ + private readonly string _key; + private readonly IEnumerable _items; + + public Issue22320ItemGroup(string key, IEnumerable items) + { + _key = key; + _items = items; + } + + public string Key => _key; + + public IEnumerator GetEnumerator() => _items.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); +} + +public class Issue22320ViewModel +{ + public Issue22320ViewModel() + { + + Items.Add(new Issue22320ItemGroup("Vegetables", new[] { "Carrot", "Broccoli", "Spinach", "Potato", "Tomato" })); + Items.Add(new Issue22320ItemGroup("Fruits", new[] { "Apple", "Banana", "Orange", "Grapes", "Mango" })); + } + + public ObservableCollection Items { get; } = new(); +} + + diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue22320.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue22320.cs new file mode 100644 index 000000000000..bdc3b98866c3 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue22320.cs @@ -0,0 +1,24 @@ +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +public class Issue22320 : _IssuesUITest +{ + public Issue22320(TestDevice device) : base(device) + { + } + + public override string Issue => "CollectionView does not highlight selected grouped items correctly"; + + [Test] + [Category(UITestCategories.CollectionView)] + public void SelectionShouldNotMovedToTopWithGroupedCollection() + { + App.WaitForElement("CollectionView"); + App.Tap("Grapes"); + App.Tap("Potato"); + VerifyScreenshot(); + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/SelectionShouldNotMovedToTopWithGroupedCollection.png b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/SelectionShouldNotMovedToTopWithGroupedCollection.png new file mode 100644 index 000000000000..f4fd695fc922 Binary files /dev/null and b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/SelectionShouldNotMovedToTopWithGroupedCollection.png differ