Closed MaxenceSAUNIER closed 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:
@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>
.
@mshwf This is being addressed by #8323
CollectionView already supports two-way binding for SelectedItems.
Your sample project has a few problems. First,
MyObjectsSelected
in your viewmodel is of typeObservableCollection<MyObject>
. You need to change the type in your viewmodel toObservableCollection<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 yourObservableCollection
; for that, you need to be subscribed to theCollectionChanged
event.Here's an updated version of your sample that illustrates a way to accomplish your goal:
@hartez I think this has regressed as InitialSelection doesn't seem to work anymore; please see https://github.com/xamarin/Xamarin.Forms/issues/13592.
Summary
Add TwoWay possibility with a CollectionView MultipleSelection and with CollectionView list
Example: CollectionViewSample.zip