xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Enhancement] MultipleSelection in CollectionView SelectedItems Mode TwoWay. #6891

Closed MaxenceSAUNIER closed 5 years ago

MaxenceSAUNIER commented 5 years ago

Summary

Add TwoWay possibility with a CollectionView MultipleSelection and with CollectionView list

Example: CollectionViewSample.zip

hartez commented 5 years ago

CollectionView already supports two-way binding for SelectedItems.

Your sample project has a few problems. First, MyObjectsSelected in your viewmodel is of type ObservableCollection<MyObject>. You need to change the type in your viewmodel to ObservableCollection<object>; otherwise, you'll see a binding error (something like "cannot convert to type") and the binding will fail.

Second, you are never calling OnPropertyChanged in your viewmodel. And if you were, you still wouldn't see changes to the contents of your ObservableCollection; for that, you need to be subscribed to the CollectionChanged event.

Here's an updated version of your sample that illustrates a way to accomplish your goal:

CollectionViewSample_updated.zip

mshwf commented 4 years ago

@hartez It seems it's only possible to bind SelectedItems to ObservableCollection<object> for example if the ItemsSource of type ObservableCollection<MyObject> then MyObjectsSelected can't be of that type, it only works if it is ObservableCollection<object>.

hartez commented 4 years ago

@mshwf This is being addressed by #8323

Tommigun1980 commented 3 years ago

CollectionView already supports two-way binding for SelectedItems.

Your sample project has a few problems. First, MyObjectsSelected in your viewmodel is of type ObservableCollection<MyObject>. You need to change the type in your viewmodel to ObservableCollection<object>; otherwise, you'll see a binding error (something like "cannot convert to type") and the binding will fail.

Second, you are never calling OnPropertyChanged in your viewmodel. And if you were, you still wouldn't see changes to the contents of your ObservableCollection; for that, you need to be subscribed to the CollectionChanged event.

Here's an updated version of your sample that illustrates a way to accomplish your goal:

CollectionViewSample_updated.zip

@hartez I think this has regressed as InitialSelection doesn't seem to work anymore; please see https://github.com/xamarin/Xamarin.Forms/issues/13592.