Home » WPFRSS

WPF Datagrid with multiple row selection/checkbox column template

Hello,
I am using the WPF Toolkit Datagrid with a checkbox defined in a template column/template cell. We are using the checkbox template so that selecting the checkbox will also highlight the row in the datagrid. Here is the template definition:

<dg:DataGridTemplateColumn MinWidth="50" Width="Auto" Header="" CanUserReorder="False">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsCheckedm, Mode=TwoWay}" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>


We are allowing multiple row selection in the datagrid and using MVVM (using MVVM Light). We would like to keep a list in the View Model of selected rows as they are selected/unselected. Any suggestions on how we can leverage MVVM to keep the list of selected/unselected elements current.

Many Thanks
--Sam

 

3 Answers Found

 

Answer 1

Hi wildturtle,

Based on my understanding, you have a list  in the View Model that it has been bound to the DataGrid. And the Item in the List has a property named IsCheckedm which saves the status of the CheckBox. You would like to keep this list in the View Model of the selected/unselected rows.

Well, there is a solution for this scenario. You could set a data binding on the DataGridRow.IsSelected property to the "IsCheckedm" property too, or you could create a new property bound to the DataGridRow.IsSelected that saves the selected  rows in the List.

<dg:DataGridSelectionMode="Extended"><dg:DataGrid.RowStyle><StyleTargetType="{x:Type dg:DataGridRow}"><SetterProperty="IsSelected"Value="{Binding IsSelected}"/><!-- or the IsCheckedm property --></Style></dg:DataGrid.RowStyle><dg:DataGrid.Columns><dg:DataGridTemplateColumn.../></dg:DataGrid.Columns></dg:DataGrid>

Now, in the List which the IsSelected property equals true are the selected Row, and others are the unselected.

Hope this helps.

Sincerely,

Bob Bao

MSDN Subscriber Support in Forum 

If you have any feedback on our support, please contact msdnmg@microsoft.com

 

Answer 2

 

Hi  wildturtle,

 

How about your problem? If you need help, please feel free to let me know.

 

Sincerely,

Bob Bao

 

Answer 3

Hi Bob,

Thanks for your kind follow-up. Ended up using a behavior attached to the DataGrid for enabling multi-selection and that did what we needed. Saving the status of the checkbox  in a property works, but for my purposes (which I forgot to include in my original problem statement :) ), the challenge with this is with a large dataset looping  through all underlying rows  becomes a performance hit. I am also using MVVM, so didn't want to directly attach a change event to the checkbox.

Appreciate your prompt response and support!

All the best,

--Sam

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter