Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Enhancement] Allow to maually deselect item from CollectionView #6158

Closed
HobDev opened this issue May 10, 2019 · 12 comments · Fixed by #6085
Closed

[Enhancement] Allow to maually deselect item from CollectionView #6158

HobDev opened this issue May 10, 2019 · 12 comments · Fixed by #6085

Comments

@HobDev
Copy link

HobDev commented May 10, 2019

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.

@pinoelefante
Copy link

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;
    }
}

@hartez
Copy link
Contributor

hartez commented May 15, 2019

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

@hartez hartez closed this as completed May 15, 2019
@hartez hartez added this to Closed in Enhancements via automation May 15, 2019
samhouts pushed a commit that referenced this issue May 28, 2019
…orrectly (#6085)

* Add automated test for CollectionView single selection bound item

* Make SelectedItem Two-Way

* Multiple selection test page

* Bindable SelectedItems implementation

* Add automated test

* Simplify null checks

* Add Preserve attribute so linker doesn't break test

* Make multi-item select test smaller so it passes UITests on smaller screens

* Clearer list-to-string method

* Clear native selection on iOS when SelectedItem set to null

fixes #6158
fixes #5832
@samhouts samhouts added this to In Progress in v4.2.0 May 29, 2019
@samhouts samhouts moved this from In Progress to Done in v4.2.0 May 29, 2019
@samhouts samhouts removed this from Closed in Enhancements May 29, 2019
@samhouts samhouts removed this from Done in v4.2.0 Jun 4, 2019
@samhouts samhouts added this to In Progress in v4.1.0 Jun 4, 2019
@samhouts samhouts moved this from In Progress to Done in v4.1.0 Jun 5, 2019
@BrayanKhosravian
Copy link
Contributor

this seems not to work anymore for me

@BrayanKhosravian
Copy link
Contributor

setting the selected item to null dosnt reset the selection

@hartez
Copy link
Contributor

hartez commented Jan 17, 2020

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

@BrayanKhosravian
Copy link
Contributor

BrayanKhosravian commented Jan 17, 2020

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?

@BrayanKhosravian
Copy link
Contributor

im online on gitter as well

@hartez
Copy link
Contributor

hartez commented Jan 17, 2020

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: #8842

@BrayanKhosravian
Copy link
Contributor

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;
	    }

@BrayanKhosravian
Copy link
Contributor

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.

@Tuytje
Copy link

Tuytje commented Nov 25, 2021

I had the same problem and found a solution by wrapping the SelectedItem = null in a UI call for UWP:
Device.BeginInvokeOnMainThread(() => SelectedItem = null);

I'm working with a ViewModel with a TwoWay Binded SelectedItem

@drejoh63
Copy link

drejoh63 commented Dec 9, 2021

I had the same problem and found a solution by wrapping the SelectedItem = null in a UI call for UWP: Device.BeginInvokeOnMainThread(() => SelectedItem = null);

I'm working with a ViewModel with a TwoWay Binded SelectedItem

This solved my issue

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
v4.1.0
  
Done
Development

Successfully merging a pull request may close this issue.

7 participants