Xamarin.Forms icon indicating copy to clipboard operation
Xamarin.Forms copied to clipboard

[UWP] Cannot unselect SelectedItem for CollectionView

Open Agredo opened this issue 4 years ago • 9 comments

Description

Unselecting SelectedItem in MVVM by setting the binded property to null does work for iOS and Android for UWP it does not work.

My case: Selecting a Item navigates to another page. After navigating back the selection should be unselected.

SelectionChangedCommand:

        private async void ArticleTeaserCollectionSelectionCommandMethod()
        {
            if(SelectedArticleTeaserItem != null && SelectedArticleTeaserItem != default)
            {
                await Navigation.PushAsync(new FullArticlePage(Navigation, new ArticleTeaser
                {
                    Title = SelectedArticleTeaserItem.Title,
                    ArticleLink = SelectedArticleTeaserItem.ArticleLink,
                    Date = SelectedArticleTeaserItem.Date,
                    TeaserText = SelectedArticleTeaserItem.TeaserText,
                    TeaserImageLink = SelectedArticleTeaserItem.TeaserImageLink
                }), true);

                SelectedArticleTeaserItem = null;
            }
        }

Sample.zip

Steps to Reproduce

  1. Create a SelectedItem property
  2. Create a SelectionChangedCommand
  3. Fill a CollectionView
  4. Clear Selection in SelectionChangedCommand

Expected Behavior

Selection should be cleared for UWP

Actual Behavior

UWP Selection does not clear (iOS and Android works)

Basic Information

Xamarin.Forms 4.7

Agredo avatar Jul 11 '20 10:07 Agredo

I have tried to reproduce the issue. Using SelectionChangedCommand command I set SelectedItem to null after one second. issue11405 What Xamarin.Forms version are you using?

jsuarezruiz avatar Jul 13 '20 12:07 jsuarezruiz

Sample.zip

@jsuarezruiz Here is a sample. As I wrote I does not work this way I showed.

Agredo avatar Jul 19 '20 21:07 Agredo

Same issue but only with Selected VisualSate defined. If VSM not defined on CollectionView, the item is unselected correctly. Related to PR https://github.com/xamarin/Xamarin.Forms/pull/10770

YZahringer avatar Aug 01 '20 16:08 YZahringer

I'm also experiencing this issue, this also seems related to bug #12491

dmoor-viewpoint avatar Nov 16 '20 14:11 dmoor-viewpoint

i am still facing this issue. I tried to:

  • Set selected item to null
  • Clear the list
  • set the whole list to null
  • set it to a new empty observable collection.

Nothing works. Only recreating the whole page reset the selection. I am using Xamarin forms version: 5.0.0.2012

ali-h2010 avatar Dec 05 '21 10:12 ali-h2010

Surprisingly, a simple Task.Delay(5000) resolved this issue for me as @jsuarezruiz mentioned.

await Task.Delay(5000); RecommandedAttendeesList.SelectedItem = null;

ali-h2010 avatar Dec 05 '21 12:12 ali-h2010

I was able to reduce that delay to 50ms. That's mostly undetectable. It would be nice if this issue is properly addressed but it's a great workaround you found! saved my life!

everettpatel avatar Feb 20 '22 04:02 everettpatel

Adding a timer like that isn't a fix. It's a bad workaround that isn't production grade. We need a real workaround or real fix.

nbevans avatar Jul 08 '22 13:07 nbevans

Description

Unselecting SelectedItem in MVVM by setting the binded property to null does work for iOS and Android for UWP it does not work.

My case: Selecting a Item navigates to another page. After navigating back the selection should be unselected.

SelectionChangedCommand:

        private async void ArticleTeaserCollectionSelectionCommandMethod()
        {
            if(SelectedArticleTeaserItem != null && SelectedArticleTeaserItem != default)
            {
                await Navigation.PushAsync(new FullArticlePage(Navigation, new ArticleTeaser
                {
                    Title = SelectedArticleTeaserItem.Title,
                    ArticleLink = SelectedArticleTeaserItem.ArticleLink,
                    Date = SelectedArticleTeaserItem.Date,
                    TeaserText = SelectedArticleTeaserItem.TeaserText,
                    TeaserImageLink = SelectedArticleTeaserItem.TeaserImageLink
                }), true);

                SelectedArticleTeaserItem = null;
            }
        }

Sample.zip

Steps to Reproduce

  1. Create a SelectedItem property
  2. Create a SelectionChangedCommand
  3. Fill a CollectionView
  4. Clear Selection in SelectionChangedCommand

Expected Behavior

Selection should be cleared for UWP

Actual Behavior

UWP Selection does not clear (iOS and Android works)

Basic Information

Xamarin.Forms 4.7

Use OnPropertyChanged(); on SelectedItem.

See this https://learn.microsoft.com/en-us/answers/questions/221399/collectionview-deselection

public Monkey SelectedMonkey { get { return selectedMonkey; } set { if (selectedMonkey != value) { selectedMonkey = value; OnPropertyChanged(); } } }

angelieri avatar Nov 10 '23 00:11 angelieri