|
1 | | -# How to change the checkbox column values based on selection in wpf treegrid? |
2 | | -This sample illustrates how to change the checkbox column values based on selection in wpf treegrid |
| 1 | +# How to change the checkbox column values based on selection in wpf treegrid |
| 2 | + |
| 3 | +This sample illustrates how to change the checkbox column values based on selection in [WPF TreeGrid](https://www.syncfusion.com/wpf-controls/treegrid). |
| 4 | + |
| 5 | +In `TreeGrid`, you can select multiple rows using the [SelectionMode](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Grid.SfGridBase.html#Syncfusion_UI_Xaml_Grid_SfGridBase_SelectionMode) property. You can process the CheckBoxSelection using [TreeGridCheckBoxColumn](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.TreeGrid.TreeGridCheckBoxColumn.html) and a boolean property called `IsSelected` in Model. You can also select all the rows in `TreeGrid` by defining the CheckBox in header cell of `GridCheckBoxColumn` using [GridColumn.HeaderTemplate](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.TreeGrid.SfTreeGrid.html#Syncfusion_UI_Xaml_TreeGrid_SfTreeGrid_HeaderTemplate). |
| 6 | + |
| 7 | +``` c# |
| 8 | +public static class Commands |
| 9 | +{ |
| 10 | + static Commands() |
| 11 | + { |
| 12 | + CommandManager.RegisterClassCommandBinding(typeof(CheckBox), new CommandBinding(CheckAndUnCheck, OnCheckUnCheckCommand, OnCanExecuteCheckAndUnCheck)); |
| 13 | + } |
| 14 | + |
| 15 | + public static RoutedCommand CheckAndUnCheck = new RoutedCommand("CheckAndUnCheck", typeof(CheckBox)); |
| 16 | + |
| 17 | + private static void OnCheckUnCheckCommand(object sender, ExecutedRoutedEventArgs args) |
| 18 | + { |
| 19 | + var treegrid = (args.Parameter as SfTreeGrid); |
| 20 | + var viewmodel = (treegrid.DataContext as ViewModel); |
| 21 | + var checkbox = (sender as CheckBox).IsChecked; |
| 22 | + if (viewmodel != null) |
| 23 | + { |
| 24 | + if (checkbox == true) |
| 25 | + { |
| 26 | + treegrid.SelectAll(); |
| 27 | + foreach (var collection in viewmodel.EmployeeInfo) |
| 28 | + { |
| 29 | + if (collection.IsSelected == false) |
| 30 | + collection.IsSelected = true; |
| 31 | + } |
| 32 | + } |
| 33 | + else if (checkbox == false) |
| 34 | + { |
| 35 | + treegrid.ClearSelections(false); |
| 36 | + foreach (var collection in viewmodel.EmployeeInfo) |
| 37 | + { |
| 38 | + if (collection.IsSelected == true) |
| 39 | + collection.IsSelected = false; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private static void OnCanExecuteCheckAndUnCheck(object sender, CanExecuteRoutedEventArgs args) |
| 46 | + { |
| 47 | + args.CanExecute = true; |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
0 commit comments