kastrup312
kastrup312

Reputation: 33

In MAUI collectionview, cannot clear selection to enable SelectionChanged on same item

In MAUI, I have a simple CollectionView and I would like the SelectionChanged event to be raised even when I press the same item again. So in the SelectionChanged handler I make sure to clear the selection. But the event is not raised again.

CollectionView defined as defined as:

<CollectionView x:Name="colView"
    ItemsSource="{Binding PickGroups}"
    IsGrouped="True"
    ItemTemplate="{StaticResource DBDataTemplate}"
    SelectionMode="Single"
    SelectionChanged="colView_SelectionChanged">

    <!-- ... -->

</CollectionView>

The handler defined as:

private async void colView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var selectedPick = e.CurrentSelection.FirstOrDefault() as Pick;
    if (selectedPick == null) return; // after a clear selection

    //... do stuff ...

    colView.SelectedItems = null; // pressing same item should now fire again, but it doesn't.
}

I expected that clearing the selection in the handler would make the event fire again when pressing the same item in the CollectionView.

Upvotes: 1

Views: 709

Answers (1)

Jessie Zhang -MSFT
Jessie Zhang -MSFT

Reputation: 13879

You can set the SelectedItem(not SelectedItems ) of your CollectionView to null :

if(yourCollectionView.SelectedItem !=null)
{

  //add other logic code here

  //colView.SelectedItems = null;
   yourCollectionView.SelectedItem = null;

 }

Upvotes: 2

Related Questions