Skip to content

Commit d549985

Browse files
SyedAbdulAzeemSF4852rmarinho
authored andcommitted
[Windows] Fix for Assigning null to the SelectedItem of the CollectionView in the SelectionChanged event does not clear the selection (#29288)
* Fix for 10025 (Setting the CollectionView's SelectedItem to null on selection changed event does nothing) * Have added the test case * Have added the snapshots
1 parent a77112b commit d549985

File tree

5 files changed

+83
-2
lines changed

5 files changed

+83
-2
lines changed

src/Controls/src/Core/Handlers/Items/SelectableItemsViewHandler.Windows.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace Microsoft.Maui.Controls.Handlers.Items
1414
public partial class SelectableItemsViewHandler<TItemsView> : StructuredItemsViewHandler<TItemsView> where TItemsView : SelectableItemsView
1515
{
1616
bool _ignorePlatformSelectionChange;
17+
bool _ignoreVirtualSelectionChange;
1718

1819
protected override void ConnectHandler(ListViewBase platformView)
1920
{
@@ -132,6 +133,13 @@ void UpdatePlatformSelection()
132133

133134
void VirtualSelectionChanged(object sender, SelectionChangedEventArgs e)
134135
{
136+
// When the selection changes within the SelectionChanged event, the new selection isn't immediately reflected in the view.
137+
// After the virtual selection is correctly updated, the flag is reset to enable future updates
138+
if (_ignoreVirtualSelectionChange)
139+
{
140+
_ignoreVirtualSelectionChange = false;
141+
return;
142+
}
135143
UpdatePlatformSelection();
136144
}
137145

@@ -172,10 +180,10 @@ void UpdateVirtualSingleSelection()
172180

173181
if (ItemsView != null)
174182
{
175-
ItemsView.SelectionChanged -= VirtualSelectionChanged;
183+
_ignoreVirtualSelectionChange = true;
176184
ItemsView.SelectedItem = selectedItem;
177185

178-
ItemsView.SelectionChanged += VirtualSelectionChanged;
186+
_ignoreVirtualSelectionChange = false;
179187
}
180188
}
181189

16.1 KB
Loading
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace Controls.TestCases.HostApp.Issues;
2+
3+
[Issue(IssueTracker.Github, 10025, "Assigning null to the SelectedItem of the CollectionView in the SelectionChanged event does not clear the selection as expected", PlatformAffected.UWP)]
4+
public class Issue10025 : ContentPage
5+
{
6+
CollectionView collectionView;
7+
public Issue10025()
8+
{
9+
Label descriptionLabel = new Label
10+
{
11+
AutomationId = "DescriptionLabel",
12+
Text = "The test passes if the SelectedItem is set to null and no visual selection indicator is displayed; otherwise, it fails",
13+
Margin = new Thickness(10),
14+
};
15+
16+
collectionView = new CollectionView
17+
{
18+
SelectionMode = SelectionMode.Single,
19+
ItemsSource = new List<string> { "Item1", "Item2" },
20+
ItemTemplate = new DataTemplate(() =>
21+
{
22+
Label label = new Label();
23+
24+
label.SetBinding(Label.TextProperty, ".");
25+
label.SetBinding(Label.AutomationIdProperty, ".");
26+
27+
return label;
28+
})
29+
};
30+
31+
collectionView.SelectionChanged += SelectionChangedEvent;
32+
33+
Content = new VerticalStackLayout
34+
{
35+
Padding = new Thickness(20),
36+
Children =
37+
{
38+
descriptionLabel,
39+
collectionView
40+
}
41+
};
42+
}
43+
44+
private void SelectionChangedEvent(object sender, SelectionChangedEventArgs e)
45+
{
46+
collectionView.SelectedItem = null;
47+
}
48+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#if TEST_FAILS_ON_IOS && TEST_FAILS_ON_CATALYST // Select items traces are preserved Issue Link - https://github.com/dotnet/maui/issues/26187
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.TestCases.Tests.Issues;
7+
8+
public class Issue10025 : _IssuesUITest
9+
{
10+
public Issue10025(TestDevice device) : base(device)
11+
{
12+
}
13+
14+
public override string Issue => "Assigning null to the SelectedItem of the CollectionView in the SelectionChanged event does not clear the selection as expected";
15+
16+
[Test]
17+
[Category(UITestCategories.CollectionView)]
18+
public void VerifySelectedItemClearsOnNullAssignment()
19+
{
20+
App.Tap("Item1");
21+
App.Tap("DescriptionLabel");
22+
VerifyScreenshot();
23+
}
24+
}
25+
#endif
9.05 KB
Loading

0 commit comments

Comments
 (0)