Xamarin.forms: [Enhancement] Allow to maually deselect item from CollectionView

Created on 10 May 2019  Â·  10Comments  Â·  Source: xamarin/Xamarin.Forms

Summary

In many scenarios developer would require to programmatically needs to deselect a selected item. I tried to do so by binding SelectedItem to a property in ViewModel. I am assigning it a value of null to deselect the selected item. But that does not work.

collectionview proposal-open enhancement âž•

Most helpful comment

Once PR #6085 is merged, setting SelectedItem to null will clear the selection.

All 10 comments

I am using a workaround. I have added a Tapped event handler to the ItemTemplate and using this code

private void ItemTapped(object sender, EventArgs e)
{
    if (e is TappedEventArgs)
    {
        var ev = e as TappedEventArgs;
        var collection = ((sender as Element).Parent as CollectionView);

        if (collection.SelectedItem == ev.Parameter)
            collection.SelectedItem = null;
        else
            collection.SelectedItem = ev.Parameter;
    }
}

Once PR #6085 is merged, setting SelectedItem to null will clear the selection.

this seems not to work anymore for me

setting the selected item to null dosnt reset the selection

@BrayanKhosravian Which version of Forms? And on which platform?

Wow that was a very fast response. Thanks :D

Latest stable release of xamarin forms (4.4.0.99142).
Tested on UWP.

But before you invest some time to investigate, let me check it one more time.
I just switched from listview to collectionview.

Is it fine for you to notify you here?

im online on gitter as well

Is it fine for you to notify you here?

This is always the correct place for that.

Also, since you said it was happening on UWP, I wonder if you're seeing this bug: https://github.com/xamarin/Xamarin.Forms/issues/8842

oh yes its exactly the bug.
i tried to unselect it by multiple ways.

private void IngredientSelected(object sender, SelectedItemChangedEventArgs e)
        {
            if(e is null) return;
            ((CollectionView) sender).SelectedItem = null;
                    e.CurrentSelection = null;
                    IngredientCollectionView.SelectedItem = null;
        }

but none worked actually. i tought that i made a mistake in my source.
but i actually found another issue as well.
but its not related to this bug report.

Was this page helpful?
0 / 5 - 0 ratings