|
1 | | -# How to merge cells in a row in wpf treegrid? |
2 | | -This example explains how to merge cells in a row in wpf treegrid |
| 1 | +# How to merge cells in a row in wpf treegrid |
| 2 | + |
| 3 | +This example explains how to merge cells in a row in [WPF TreeGrid](https://www.syncfusion.com/wpf-controls/treegrid). |
| 4 | + |
| 5 | +You can merge the entire column parent node using `TreeGridCoveredCellInfo`. |
| 6 | + |
| 7 | +### XAML |
| 8 | + |
| 9 | +``` xml |
| 10 | +<syncfusion:SfTreeGrid Name="treeGrid" |
| 11 | + ItemsSource="{Binding EmployeeDetails}" |
| 12 | + QueryCoveredRange="treeGrid_QueryCoveredRange" |
| 13 | + SelectionMode="Single" |
| 14 | + NavigationMode="Cell"> |
| 15 | +``` |
| 16 | + |
| 17 | +### C# |
| 18 | + |
| 19 | +``` c# |
| 20 | +public delegate void TreeGridRequestTreeItemsHandler(object sender, RoutedEventArgs args); |
| 21 | + |
| 22 | +/// <summary> |
| 23 | +/// Handles the cell merging in SfTreeGrid. |
| 24 | +/// </summary> |
| 25 | +public class QueryCoveredRangeBehavior : Behavior<SfTreeGrid> |
| 26 | +{ |
| 27 | + /// <summary> |
| 28 | + /// Called after the behavior is attached to an AssociatedObject. |
| 29 | + /// </summary> |
| 30 | + /// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks> |
| 31 | + protected override void OnAttached() |
| 32 | + { |
| 33 | + var loader= new TreeGridRequestTreeItemsHandler(AssociatedObject_Loaded); |
| 34 | + loader.Invoke(null, null); |
| 35 | + } |
| 36 | + |
| 37 | + public void AssociatedObject_Loaded(object sender, RoutedEventArgs e) |
| 38 | + { |
| 39 | + this.AssociatedObject.QueryCoveredRange += AssociatedObject_QueryCoveredRange; |
| 40 | + } |
| 41 | + |
| 42 | + public void AssociatedObject_QueryCoveredRange(object sender, TreeGridQueryCoveredRangeEventArgs e) |
| 43 | + { |
| 44 | + var treeNode = this.AssociatedObject.GetNodeAtRowIndex(e.RowColumnIndex.RowIndex); |
| 45 | + if (treeNode != null && treeNode.HasChildNodes) |
| 46 | + { |
| 47 | + if (e.RowColumnIndex.ColumnIndex >= 1 && e.RowColumnIndex.ColumnIndex <= this.AssociatedObject.Columns.Count) |
| 48 | + { |
| 49 | + e.Range = new TreeGridCoveredCellInfo(0, this.AssociatedObject.Columns.Count, e.RowColumnIndex.RowIndex); |
| 50 | + e.Handled = true; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Calls when the behavior is being detached from its AssociatedObject, but before it has actually occurred. |
| 57 | + /// </summary> |
| 58 | + /// <remarks>Override this to unhook functionality from the AssociatedObject.</remarks> |
| 59 | + protected override void OnDetaching() |
| 60 | + { |
| 61 | + base.OnDetaching(); |
| 62 | + this.AssociatedObject.Loaded -= AssociatedObject_Loaded; |
| 63 | + this.AssociatedObject.QueryCoveredRange -= AssociatedObject_QueryCoveredRange; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +class RequestTreeItemsBehavior : Behavior<SfTreeGrid> |
| 68 | +{ |
| 69 | + EmployeeRepository viewModel; |
| 70 | + |
| 71 | + protected override void OnAttached() |
| 72 | + { |
| 73 | + base.OnAttached(); |
| 74 | + viewModel = this.AssociatedObject.DataContext as EmployeeRepository; |
| 75 | + this.AssociatedObject.RequestTreeItems += AssociatedObject_RequestTreeItems; |
| 76 | + } |
| 77 | + |
| 78 | + void AssociatedObject_RequestTreeItems(object sender, TreeGridRequestTreeItemsEventArgs args) |
| 79 | + { |
| 80 | + if (args.ParentItem == null) |
| 81 | + { |
| 82 | + // Gets the root list - Gets all employees who have no boss. |
| 83 | + args.ChildItems = EmployeeRepository.GetEmployees().Where(x => x.ReportsTo == -1); //get all employees whose boss's id is -1 (no boss) |
| 84 | + } |
| 85 | + else //if ParentItem not null, then set args.ChildList to the child items for the given ParentItem. |
| 86 | + { |
| 87 | + // Gets the children of the parent object. |
| 88 | + Employee emp = args.ParentItem as Employee; |
| 89 | + if (emp != null) |
| 90 | + { |
| 91 | + // Gets all employees who report to the parent employee. |
| 92 | + args.ChildItems = EmployeeRepository.GetEmployees().Where(x => x.ReportsTo == emp.Id); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + protected override void OnDetaching() |
| 98 | + { |
| 99 | + base.OnDetaching(); |
| 100 | + this.AssociatedObject.RequestTreeItems -= AssociatedObject_RequestTreeItems; |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
3 | 104 |
|
4 | 105 | Note: |
5 | 106 | Supports cell merging from 17.2.0.28 version |
0 commit comments