We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Two way binding for SelectedItems in SfDataGrid

Hi,
I have a ViewModel that has a property, ReactiveList SelectedItems, which I want to bind to the SfDataGrid.SelectedItems property.

It needs to be a two way binding:
  • when a user selects multiple items on the SfDataGrid, my ViewModel should receive notification that the collection of selected items has changed.
  • if I programmatically set ViewModel.SelectedItems, then the SfDataGrid should reflect this selection.
I have tried using the following technique, https://www.syncfusion.com/kb/3070/how-to-bind-the-selecteditems-property-of-sfdatagrid-to-viewmodel-property, but that doesn't work because:
  • The binding is only one way, and badly implemented since it creates a new ObservableCollection each time, rather than updating the existing ObservableCollection. By recreating the ObservableCollection each time, any existing handlers attached to the ViewModel property will no longer work. Also, as noted in the comments below the article, 'The OnSelectedItemsChanged method is called multiple times after the first selection. This effectively renders any usage impractical.'
  • It uses an ObservableCollection, not a ReactiveList.
I also found this solution, but it applies to Xamarin, not WPF, https://www.syncfusion.com/forums/127622/how-to-bind-selected-items-in-sfdatagrid-to-viewmodel.
Also, I am unable to test whether this particular approach works with ReactiveLists, or works in a two way fashion.

How can I achieve this without breaking MVVM?

Cheers,
James.

3 Replies

AA Arulraj A Syncfusion Team February 20, 2019 05:31 PM UTC

Hi James, 

Thanks for using Syncfusion product. 

We are working on the requested requirement and we need one more business day to complete it and we will update you with the details on 21 February 2019. 

We appreciate your patience until then. 

Regards, 
Arulraj A 



MA Mohanram Anbukkarasu Syncfusion Team February 21, 2019 01:20 PM UTC

Hi James, 

Thanks for your patience. 

We have created a simple sample to achieve your requirement by updating the existing ObservableCollection rather than creating new one everytime. Please refer the sample from the following link. 


We are unable to provide this sample with ReactiveList. We are currently work on this. We will update you with further details on February 22, 2019. We appreciate your patience until then. 

Regards, 
Mohanram A. 



JN Jayaleshwari N Syncfusion Team February 22, 2019 11:56 AM UTC

Hi James, 
 
Thanks for your patience. 
 
We are little bit unclear in creating the ReactiveList you have used to bind to the SfDataGrid.SelectedItems property. Could you please provide more details about ReactiveList with code example/sample? It will be helpful for us to check on this and provide an appropriate solution to your requirement at earlier. 
 
Regards, 
Jayaleshwari N 


Loader.
Live Chat Icon For mobile
Up arrow icon