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

SelectedItems TwoWay Binding not working

Hi,

i have downloaded your sample for MVVM implementation of SelectedItems in SfDatagrid from here: https://help.syncfusion.com/wpf/datagrid/mvvm

When running the sample, the items which are selected in the Constructor of the Viewmodel are shown as selected in the Grid.
However, if i select additional rows in the grid, the SelectedItems property setter gets never called.

could you please confirm  if this is a bug?

thanks,

Helmut

5 Replies

SS Susmitha Sundar Syncfusion Team December 31, 2019 12:27 PM UTC

Hi Helmut,   
   
Thank you for using Syncfusion controls.   
   
We have checked the reported issue in our sample(https://help.syncfusion.com/wpf/datagrid/mvvm#datagrid-selecteditem-binding), and we are unable to replicate the issue from our end. SelectedItems setter property called for every selection. We have prepared a video for your reference, 
   
 
We have tested with Syncfusion version 17.4.0.39.   
   
Please check the above link and revert us if you still facing the same issue? If yes, please modify the sample based on your scenario. It will be helpful for us to check on it and provide you the solution at the earliest.     
   
Regards,   
Susmitha S   



HW Helmut Wahrmann January 3, 2020 09:47 AM UTC

Hi Susmitha,

i was referring to SelectedItems  and not SelectedItem.

btw, if you use the SelectedItem sample and you select multiple rows, the SelectedItem only contains the first selected item and not the row, which you just selected.

thanks,

Helmut


SS Susmitha Sundar Syncfusion Team January 6, 2020 02:17 PM UTC

Hi Helmut, 
 
Thank you for your update. 
 
In SfDataGrid, SelectedItems property is collection type. So, if you click any row, SelectedItems collectionchanged event called. Please refer the below image, 
 
Screenshot: 
 
 
 
 
 
Please check the sample and let us know if you need further assistance on this. 
 
Regards, 
Susmitha S 



HW Helmut Wahrmann January 6, 2020 06:53 PM UTC

Hi Susmitha,

so you created some code behind.
I was referring to your MVVM samples and here the "set" on line 21 in PersonModel.cs is never called, when you select something.

regards,

Helmut


SS Susmitha Sundar Syncfusion Team January 7, 2020 07:09 AM UTC

Hi Helmut,   
  
Thank you for your update.   
  
In SfDataGrid, SelectedItems is ObseravableCollection of object. If you want to call its setter, you need to assign the collection to that property. Please refer the below code snippet  
C#:   
ObservableCollection<object> selectedItemsCollection = newObservableCollection<object>();   
PersonModel viewModel;   
public MainWindow()   
{   
    InitializeComponent();   
    viewModel = this.DataContext as PersonModel;   
    selectedItemsCollection.Add(viewModel.PersonDetails[1]);   
    selectedItemsCollection.Add(viewModel.PersonDetails[2]);   
    selectedItemsCollection.Add(viewModel.PersonDetails[3]);   
    selectedItemsCollection.Add(viewModel.PersonDetails[4]);   
    this.dataGrid.SelectedItems.CollectionChanged += SelectedItems_CollectionChanged;   
}   
  
private void Button1_Click(object sender, RoutedEventArgs e)   
{   
     viewModel.SelectedItems = selectedItemsCollection;   
}   
  
In our sample, when you select the items, these items are added to that collection, so its collection changed called. It’s a difference between assign and add the items. Please refer the below sample,   
  
  
Please let us know if you need further assistance on this.   
  
Regards,   
Susmitha S   


Loader.
Live Chat Icon For mobile
Up arrow icon