Posted 3 November 2019, 5:01 pm EST
Hi Mayur,
As per my understanding, you want to select/deselect a row when a checkbox is checked/unchecked in that particular row and allowing multiple rows to be selected. If so then please refer to the answer below otherwise please correct me where I am mistaken.
Since it will require to handle the check/unchek events of the checkboxes so, we’ll have to handle the LoadedCellPresenter events as follows:
private void DataGrid_LoadedCellPresenter(object sender, C1.WPF.DataGrid.DataGridCellEventArgs e)
{
if(e.Cell.Column.Name == "IsSelected")
{
(e.Cell.Presenter.Content as CheckBox).Tag = e.Cell;
(e.Cell.Presenter.Content as CheckBox).Checked += MainWindow_Checked;
(e.Cell.Presenter.Content as CheckBox).Unchecked += MainWindow_Unchecked;
}
}
Now, upon checkbox checked/unchecked events we’ll need to update the collection of selected items. Since we need to maintain selected items in a customized way so, we’ll have to keep the collection of selected items in our ViewModel and use it as follows:
private void MainWindow_Unchecked(object sender, RoutedEventArgs e)
{
C1.WPF.DataGrid.DataGridCell cell = (sender as CheckBox).Tag as C1.WPF.DataGrid.DataGridCell;
dataGrid.Selection.Remove(cell.Row, cell.Row);
vm.SelectedProducts.Remove(cell.Row.DataItem as Product);
}
private void MainWindow_Checked(object sender, RoutedEventArgs e)
{
C1.WPF.DataGrid.DataGridCell cell = (sender as CheckBox).Tag as C1.WPF.DataGrid.DataGridCell;
vm.SelectedProducts.Add(cell.Row.DataItem as Product);
}
Update DataGrid selection when our collection of selected items is updated:
private void SelectedProducts_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
foreach(var item in vm.SelectedProducts)
{
var row = dataGrid.Rows.FirstOrDefault(r => r.DataItem == item);
dataGrid.Selection.Add(row, row);
}
}
Please refer to the attached sample for the same.
Regards,
Basant
prj_SelectedRowItem_Mod2.zip