Skip to content

Commit 8815090

Browse files
NanthiniMahalingamPureWeen
authored andcommitted
Fixed the CollectionView grouped item highlighting issue on selection (#29122)
* Fixed the collection view highlighting issue * Remove unwanted code section * Added UI test case. * Updated the fix and added output images
1 parent 4d7662c commit 8815090

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

src/Controls/src/Core/Handlers/Items/iOS/ObservableGroupedSource.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ int IndexInGroup(object item, object group)
378378
{
379379
return index;
380380
}
381+
382+
index++;
381383
}
382384
return -1;
383385
}
45 KB
Loading
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System.Collections;
2+
using System.Collections.ObjectModel;
3+
4+
namespace Maui.Controls.Sample.Issues;
5+
[Issue(IssueTracker.Github, 22320, "CollectionView does not highlight selected grouped items correctly", PlatformAffected.iOS | PlatformAffected.macOS)]
6+
public class Issue22320 : ContentPage
7+
{
8+
public Issue22320()
9+
{
10+
var layout = new StackLayout();
11+
var bindingContext = new Issue22320ViewModel();
12+
var collectionView = new CollectionView();
13+
collectionView.BindingContext = bindingContext;
14+
collectionView.IsGrouped = true;
15+
collectionView.AutomationId = "CollectionView";
16+
collectionView.SelectionMode = SelectionMode.Single;
17+
18+
collectionView.ItemTemplate = new DataTemplate(() =>
19+
{
20+
var border = new Border
21+
{
22+
HeightRequest = 20
23+
};
24+
25+
var label = new Label();
26+
label.SetBinding(Label.TextProperty, ".");
27+
28+
border.Content = label;
29+
30+
return border;
31+
});
32+
33+
collectionView.GroupHeaderTemplate = new DataTemplate(() =>
34+
{
35+
var border = new Border
36+
{
37+
Background = Colors.LightGrey,
38+
HeightRequest = 20
39+
};
40+
41+
var label = new Label();
42+
label.SetBinding(Label.TextProperty, "Key");
43+
44+
border.Content = label;
45+
46+
return border;
47+
});
48+
49+
// Set the ItemsSource binding
50+
collectionView.SetBinding(CollectionView.ItemsSourceProperty, "Items");
51+
layout.Children.Add(collectionView);
52+
53+
Content = layout;
54+
}
55+
}
56+
57+
public class Issue22320ItemGroup : IEnumerable<string>
58+
{
59+
private readonly string _key;
60+
private readonly IEnumerable<string> _items;
61+
62+
public Issue22320ItemGroup(string key, IEnumerable<string> items)
63+
{
64+
_key = key;
65+
_items = items;
66+
}
67+
68+
public string Key => _key;
69+
70+
public IEnumerator<string> GetEnumerator() => _items.GetEnumerator();
71+
72+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
73+
}
74+
75+
public class Issue22320ViewModel
76+
{
77+
public Issue22320ViewModel()
78+
{
79+
80+
Items.Add(new Issue22320ItemGroup("Vegetables", new[] { "Carrot", "Broccoli", "Spinach", "Potato", "Tomato" }));
81+
Items.Add(new Issue22320ItemGroup("Fruits", new[] { "Apple", "Banana", "Orange", "Grapes", "Mango" }));
82+
}
83+
84+
public ObservableCollection<Issue22320ItemGroup> Items { get; } = new();
85+
}
86+
87+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue22320 : _IssuesUITest
8+
{
9+
public Issue22320(TestDevice device) : base(device)
10+
{
11+
}
12+
13+
public override string Issue => "CollectionView does not highlight selected grouped items correctly";
14+
15+
[Test]
16+
[Category(UITestCategories.CollectionView)]
17+
public void SelectionShouldNotMovedToTopWithGroupedCollection()
18+
{
19+
App.WaitForElement("CollectionView");
20+
App.Tap("Grapes");
21+
App.Tap("Potato");
22+
VerifyScreenshot();
23+
}
24+
}
48.2 KB
Loading

0 commit comments

Comments
 (0)