How to apply GridCheckBoxSelectorColumn SelectedItems with mvvm

Hi,

I'm trying to apply the GridCheckBoxSelectorColumn in wpf with MVVM without success.

Is there any way to implement it without using code behind event?


9 Replies

MA Mohanram Anbukkarasu Syncfusion Team July 6, 2021 02:33 PM UTC

Hi Vitor, 

We regret for the inconvenience.  

We are unclear with your requirement. We would like to let you know that the GridCheckBoxSelectorColumn is not bound with data object from the underlying data source. You can get the items selected using GridCheckBoxSelectorColumn from SfDataGrid.SelectedItems property. You can refer the below given help documentation to know more about GridCheckBoxSelectorColumn. 


Please revert to us with more details about your requirement. It will be more helpful for us to check and provide a prompt solution at earlier.  

Regards, 
Mohanram A. 




VL Vitor Lucas dos Santos replied to Mohanram Anbukkarasu July 6, 2021 03:00 PM UTC

Hi well i'm trying to access the selected items from ViewModel and i'm binding selected items from view like bellow



and in View Model i could access the selected items by PropertyChange, so i made de code in code behind 

like that

and now i'm having the following error

System.ArgumentNullException:Value cannot be null.

Parameter name: changedItems


And I would like to know if is there any way to select itens from ViewModel and not for codebehind?



MA Mohanram Anbukkarasu Syncfusion Team July 7, 2021 09:42 AM UTC

Hi Vitor, 

Thanks for the update.  

We are able to understand your scenario.  

From the provided details we suspect that you have use Model class type to create SelectedItems collection in your view model which leads to the reported problem. You can resolve this by creating the SelectedItems collection using object type as shown in the following code example.  

Code example :  

public ObservableCollection<object> SelectedItems { get; set; } 

This will bind the collection properly with the SfDataGrid.SelectedItems property and there is no need to handle the SelectionChanged event. We have prepared a simple sample to replicate this and it is available in the following link for your reference.  


Please revert to us with details, if we have misunderstood your problem still.  

Regards, 
Mohanram A. 



VL Vitor Lucas dos Santos July 7, 2021 02:16 PM UTC

Hi  Mohanram Anbukkarasu

well i did implement as the  sample sent, it  worked but i'm facing an exception,  my software has a menu and it changes the view and the datacontext, every time i change my view using the menu it brokes the software, i think it's because the datacontext changes

the error is


it's a null exception, value cannot be null. Parameter name changeItems, it there any solutions?

i'm not making any calling in code behind, everything is in the viewmodel

Thank you for your help




MA Mohanram Anbukkarasu Syncfusion Team July 8, 2021 02:28 PM UTC

Hi Vitor, 

Thanks for the update.  

We are able to understand the reported scenario. From the provided stack trace details, it seems the collection in the ViewModel which is bound to the SfDataGrid.SelectedItems property is null when the DataContext is changed which is the cause for the exception. Initialize the collection properly before binding to the SfDataGrid.SelectedItems property to resolve this issue. Please let us know if you require any other assistance from us.  

Regards, 
Mohanram A. 



VL Vitor Lucas dos Santos July 12, 2021 12:12 PM UTC

Hi thanks for the help, but even starting the collection it gave us the same error



MA Mohanram Anbukkarasu Syncfusion Team July 13, 2021 02:22 PM UTC

Hi Vitor, 

Thanks for the update.  

We are little unclear with the reported scenario. The reported issue occurs when  the property value is null in the changed DataContext. If possible please provide an issue reproducible sample to replicate the issue in our end. It will be more helpful for us to find the exact cause for the issue and to provide prompt solution.  

Regards, 
Mohanram A. 



ON Ondrej October 17, 2021 09:49 PM UTC

Hi,
I had simliar problem. I had to set Binding ItemSource before SelectedItems in xaml.  FYI


Regards

Ondřej Rada 



BT Balamurugan Thirumalaikumar Syncfusion Team October 18, 2021 01:01 PM UTC

Hi Ondrej, 
 
Thank you for the update. 
 
Balamurugan Thirumalaikumar  
 


Loader.
Up arrow icon