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

[Bug] CollectionView multiple pre-selection throws ArgumentOutOfRangeException when SelectedItems is bound to an ObservableCollection initialized inside the constructor. #6963

Closed
balazserd opened this issue Jul 24, 2019 · 4 comments
Assignees
Labels
a/collectionview e/2 🕑 2 i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often in-progress This issue has an associated pull request that may resolve it! t/bug 🐛
Projects

Comments

@balazserd
Copy link

balazserd commented Jul 24, 2019

Description

Using the application that can be downloaded here (and is also linked here, on the official CollectionView documentation page)

When creating the ObservableCollection for the SelectedItems property via the

ObservableCollection<T>(IEnumerable<T> items)

constructor, an error is shown when trying to bound the data to the CollectionView's SelectedItems property.

Steps to Reproduce

Change the implementation of the constructor in ViewModels/MonkeysViewModel.cs from this:

        public MonkeysViewModel()
        {
            source = new List<Monkey>();
            CreateMonkeyCollection();

            selectedMonkey = Monkeys.Skip(3).FirstOrDefault();
            MonkeySelectionChanged();

            SelectedMonkeys = new ObservableCollection<object>()
            {
                Monkeys[1], Monkeys[3], Monkeys[4]
            };
        }

to this:

        public MonkeysViewModel()
        {
            source = new List<Monkey>();
            CreateMonkeyCollection();

            selectedMonkey = Monkeys.Skip(3).FirstOrDefault();
            MonkeySelectionChanged();
            
            var monkeys = Monkeys.Select(m => new Monkey()
            {
                Details = m.Details,
                ImageUrl = m.ImageUrl,
                Location = m.Location,
                Name = m.Name
            });

            SelectedMonkeys = new ObservableCollection<object>(monkeys);
        }

Expected Behavior

  • Anything that matches in the list of SelectedMonkeys should highlight and anything that doesn't matches should effectively do nothing.
  • Ordering should also be tested. If SelectedItems is set first and ItemSource second then it should select the items in SelectedItems.

Actual Behavior

ArgumentOutOfRangeException is thrown for an index property (VS for Mac throws errors on the Main function, so no idea where the issue is...)

Basic Information

  • Version with issue: latest public as of today (4.1.0)
  • Last known good version: no idea.
  • IDE: VS for Mac
  • Platform Target Frameworks:
    • iOS: 12.2
    • Android: no Android project
    • UWP: no UWP project
  • Android Support Library Version:
  • Nuget Packages:
  • Affected Devices: device-independent issue, tried on multiple ones (from iPhone SE to iPad Pro).
@balazserd balazserd added s/unverified New report that has yet to be verified t/bug 🐛 labels Jul 24, 2019
@pauldipietro pauldipietro added this to New in Triage Jul 24, 2019
@balazserd balazserd changed the title [Bug] CollectionView multiple pre-selection throws ArgumentOutOfRangeException when SelectedItems is bound to an ObbservableCollection initialized inside the constructor. [Bug] CollectionView multiple pre-selection throws ArgumentOutOfRangeException when SelectedItems is bound to an ObservableCollection initialized inside the constructor. Jul 24, 2019
@PureWeen
Copy link
Contributor

PureWeen commented Jul 25, 2019

@balazserd you're instantiating new monkeys

            var monkeys = Monkeys.Select(m => new Monkey()
            {
                Details = m.Details,
                ImageUrl = m.ImageUrl,
                Location = m.Location,
                Name = m.Name
            });

            SelectedMonkeys = new ObservableCollection<object>(monkeys);

So it's not going to be able to associate your new list of monkeys with the monkeys bound to the ItemSource. You would need to select the Monkeys you want selected out of this list Monkeys or you could override GetEquals/GetHashCode on the monkey so that the system can know what it means for one monkey to equal another monkey

I'm checking what the expected behavior should be if you assign wrong items to SelectedItems though

@PureWeen PureWeen added a/collectionview e/2 🕑 2 and removed s/unverified New report that has yet to be verified labels Jul 26, 2019
@PureWeen PureWeen added this to To do (blockers) in CollectionView via automation Jul 26, 2019
@PureWeen PureWeen added this to the 4.3.0 milestone Jul 26, 2019
@PureWeen PureWeen removed this from New in Triage Jul 26, 2019
@PureWeen
Copy link
Contributor

@balazserd I updated the ticket with the desired behavior. The crash is not desired but the effect you should see is that nothing is selected

@balazserd
Copy link
Author

Alright, thank you for the help and clarification. Good luck with fixing the real issue! :)

@samhouts samhouts added the i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often label Aug 20, 2019
@samhouts samhouts removed this from the 4.3.0 milestone Aug 29, 2019
@adrianknight89
Copy link
Contributor

@PureWeen Isn't it better to throw an exception instead of ignoring those new items that are not part of ItemsSource? I'd rather see an exception and fix the issue than try to figure out why the items are not showing up. I'm not sure what the exception says, but we can give a descriptive message.

@samhouts samhouts moved this from To do (blockers) to Backlog in CollectionView Oct 8, 2019
@samhouts samhouts moved this from Backlog to To do (blockers) in CollectionView Oct 8, 2019
@hartez hartez self-assigned this Oct 8, 2019
hartez added a commit that referenced this issue Oct 9, 2019
…sh when

selection contains items not in source; fixes #6963
@samhouts samhouts added the in-progress This issue has an associated pull request that may resolve it! label Oct 9, 2019
@samhouts samhouts added this to In Progress in v4.3.0 Oct 9, 2019
@samhouts samhouts moved this from In Progress to Done in v4.3.0 Oct 10, 2019
@samhouts samhouts moved this from To do (blockers) to In Progress in CollectionView Oct 11, 2019
CollectionView automation moved this from In Progress to Done Oct 15, 2019
felipebaltazar pushed a commit to felipebaltazar/Xamarin.Forms that referenced this issue Oct 16, 2019
…ns (xamarin#7902)

* Add selection synchronization tests

* Resync native selection after ItemsSource is updated

* Update native selection on iOS when changing ItemsSource; prevent crash when
selection contains items not in source; fixes xamarin#6963

* Automated test
felipebaltazar pushed a commit to felipebaltazar/Xamarin.Forms that referenced this issue Oct 16, 2019
…ns (xamarin#7902)

* Add selection synchronization tests

* Resync native selection after ItemsSource is updated

* Update native selection on iOS when changing ItemsSource; prevent crash when
selection contains items not in source; fixes xamarin#6963

* Automated test
CliffAgius pushed a commit to CliffAgius/Xamarin.Forms that referenced this issue Dec 6, 2019
…ns (xamarin#7902)

* Add selection synchronization tests

* Resync native selection after ItemsSource is updated

* Update native selection on iOS when changing ItemsSource; prevent crash when
selection contains items not in source; fixes xamarin#6963

* Automated test
@samhouts samhouts removed this from Done in CollectionView May 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/collectionview e/2 🕑 2 i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often in-progress This issue has an associated pull request that may resolve it! t/bug 🐛
Projects
No open projects
v4.3.0
  
Done
Development

No branches or pull requests

5 participants