[Bug] CollectionView multiple pre-selection throws ArgumentOutOfRangeException when SelectedItems is bound to an ObservableCollection initialized inside the constructor. #6963
Comments
@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 |
@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 |
Alright, thank you for the help and clarification. Good luck with fixing the real issue! :) |
@PureWeen Isn't it better to throw an exception instead of ignoring those new items that are not part of |
…sh when selection contains items not in source; fixes #6963
…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
…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
…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
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:
to this:
Expected Behavior
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
The text was updated successfully, but these errors were encountered: