Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -14,6 +14,7 @@ namespace Microsoft.Maui.Controls.Handlers.Items
public partial class SelectableItemsViewHandler<TItemsView> : StructuredItemsViewHandler<TItemsView> where TItemsView : SelectableItemsView
{
bool _ignorePlatformSelectionChange;
bool _ignoreVirtualSelectionChange;

protected override void ConnectHandler(ListViewBase platformView)
{
Expand Down Expand Up @@ -132,6 +133,13 @@ void UpdatePlatformSelection()

void VirtualSelectionChanged(object sender, SelectionChangedEventArgs e)
{
// When the selection changes within the SelectionChanged event, the new selection isn't immediately reflected in the view.
// After the virtual selection is correctly updated, the flag is reset to enable future updates
if (_ignoreVirtualSelectionChange)
{
_ignoreVirtualSelectionChange = false;
return;
}
UpdatePlatformSelection();
}

Expand Down Expand Up @@ -172,10 +180,10 @@ void UpdateVirtualSingleSelection()

if (ItemsView != null)
{
ItemsView.SelectionChanged -= VirtualSelectionChanged;
_ignoreVirtualSelectionChange = true;
ItemsView.SelectedItem = selectedItem;

ItemsView.SelectionChanged += VirtualSelectionChanged;
_ignoreVirtualSelectionChange = false;
}
}

Expand Down
48 changes: 48 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue10025.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace Controls.TestCases.HostApp.Issues;

[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)]
public class Issue10025 : ContentPage
{
CollectionView collectionView;
public Issue10025()
{
Label descriptionLabel = new Label
{
AutomationId = "DescriptionLabel",
Text = "The test passes if the SelectedItem is set to null and no visual selection indicator is displayed; otherwise, it fails",
Margin = new Thickness(10),
};

collectionView = new CollectionView
{
SelectionMode = SelectionMode.Single,
ItemsSource = new List<string> { "Item1", "Item2" },
ItemTemplate = new DataTemplate(() =>
{
Label label = new Label();

label.SetBinding(Label.TextProperty, ".");
label.SetBinding(Label.AutomationIdProperty, ".");

return label;
})
};

collectionView.SelectionChanged += SelectionChangedEvent;

Content = new VerticalStackLayout
{
Padding = new Thickness(20),
Children =
{
descriptionLabel,
collectionView
}
};
}

private void SelectionChangedEvent(object sender, SelectionChangedEventArgs e)
{
collectionView.SelectedItem = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#if TEST_FAILS_ON_IOS && TEST_FAILS_ON_CATALYST // Select items traces are preserved Issue Link - https://github.com/dotnet/maui/issues/26187
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue10025 : _IssuesUITest
{
public Issue10025(TestDevice device) : base(device)
{
}

public override string Issue => "Assigning null to the SelectedItem of the CollectionView in the SelectionChanged event does not clear the selection as expected";

[Test]
[Category(UITestCategories.CollectionView)]
public void VerifySelectedItemClearsOnNullAssignment()
{
App.Tap("Item1");
App.Tap("DescriptionLabel");
VerifyScreenshot();
}
}
#endif
Loading