|
1 | 1 | # How to drag and drop rows in wpf between treegrid and treeviewadv? |
2 | | -This example illustrates how to drag and drop rows in wpf between treegrid and treeviewadv |
| 2 | + |
| 3 | +This example illustrates how to drag and drop rows in between [WPF TreeGrid](https://www.syncfusion.com/wpf-controls/treegrid) and [WPF TreeViewAdv](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.Controls.TreeViewAdv.html). |
| 4 | + |
| 5 | +You can drag and drop the items between `TreeViewAdv` and `TreeGrid` by wiring the [Drop](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.TreeGrid.TreeGridRowDragDropController.html#Syncfusion_UI_Xaml_TreeGrid_TreeGridRowDragDropController_Drop) and [DragStart](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.TreeGrid.TreeGridRowDragDropController.html#Syncfusion_UI_Xaml_TreeGrid_TreeGridRowDragDropController_DragStart) events from the [TreeGridRowDragDropController](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.TreeGrid.TreeGridRowDragDropController.html) class. |
| 6 | + |
| 7 | +``` c# |
| 8 | +private void RowDragDropController_DragStart(object sender, TreeGridRowDragStartEventArgs e) |
| 9 | +{ |
| 10 | + e.Handled = true; |
| 11 | + var dataObject = new DataObject(); |
| 12 | + dataObject.SetData("SourceTreeGrid", this.AssociatedObject.sfTreeGrid); |
| 13 | + dataObject.SetData("Nodes", e.DraggingNodes); |
| 14 | + |
| 15 | + foreach (var node in e.DraggingNodes) |
| 16 | + { |
| 17 | + if (node.HasChildNodes) |
| 18 | + { |
| 19 | + records.Add(node.Item as EmployeeInfo); |
| 20 | + GetChildNodes(node); |
| 21 | + } |
| 22 | + else |
| 23 | + { |
| 24 | + records.Add(node.Item as EmployeeInfo); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + dataObject.SetData(records); |
| 29 | + |
| 30 | + if(records!=null) |
| 31 | + DragDrop.DoDragDrop(this.AssociatedObject.sfTreeGrid, dataObject, DragDropEffects.Copy | DragDropEffects.Move); |
| 32 | + records.Clear(); |
| 33 | +} |
| 34 | + |
| 35 | +private void RowDragDropController_Drop(object sender, TreeGridRowDropEventArgs e) |
| 36 | +{ |
| 37 | + if (e.IsFromOutSideSource) |
| 38 | + { |
| 39 | + ObservableCollection<object> item = e.Data.GetData(typeof(ObservableCollection<object>)) as ObservableCollection<object>; |
| 40 | + var record = item[0] as EmployeeInfo; |
| 41 | + var dropPosition = e.DropPosition.ToString(); |
| 42 | + var newItem = new EmployeeInfo(); |
| 43 | + |
| 44 | + var rowIndex = AssociatedObject.sfTreeGrid.ResolveToRowIndex(e.TargetNode.Item); |
| 45 | + int nodeIndex = (int)rowIndex; |
| 46 | + if (dropPosition != "None" && rowIndex != -1) |
| 47 | + { |
| 48 | + if (AssociatedObject.sfTreeGrid.View is TreeGridSelfRelationalView) |
| 49 | + { |
| 50 | + var treeNode = AssociatedObject.sfTreeGrid.GetNodeAtRowIndex(rowIndex); |
| 51 | + |
| 52 | + if (treeNode == null) |
| 53 | + return; |
| 54 | + var data = treeNode.Item; |
| 55 | + AssociatedObject.sfTreeGrid.SelectionController.SuspendUpdates(); |
| 56 | + var itemIndex = -1; |
| 57 | + |
| 58 | + TreeNode parentNode = null; |
| 59 | + |
| 60 | + if (dropPosition == "DropBelow" || dropPosition == "DropAbove") |
| 61 | + { |
| 62 | + parentNode = treeNode.ParentNode; |
| 63 | + |
| 64 | + if (parentNode == null) |
| 65 | + newItem = new EmployeeInfo() { FirstName = record.FirstName, LastName = record.LastName, ID = record.ID, Salary = record.Salary, Title = record.Title, ReportsTo = -1 }; |
| 66 | + else |
| 67 | + { |
| 68 | + var parent = parentNode.Item as EmployeeInfo; |
| 69 | + newItem = new EmployeeInfo() { FirstName = record.FirstName, LastName = record.LastName, ID = record.ID, Salary = record.Salary, Title = record.Title, ReportsTo = parent.ID }; |
| 70 | + } |
| 71 | + } |
| 72 | + else if (dropPosition == "DropAsChild") |
| 73 | + { |
| 74 | + if (!treeNode.IsExpanded) |
| 75 | + AssociatedObject.sfTreeGrid.ExpandNode(treeNode); |
| 76 | + parentNode = treeNode; |
| 77 | + var parent = parentNode.Item as EmployeeInfo; |
| 78 | + newItem = new EmployeeInfo() { FirstName = record.FirstName, LastName = record.LastName, ID = record.ID, Salary = record.Salary, Title = record.Title, ReportsTo = parent.ID }; |
| 79 | + |
| 80 | + } |
| 81 | + IList sourceCollection = null; |
| 82 | + |
| 83 | + if (dropPosition == "DropBelow" || dropPosition == "DropAbove") |
| 84 | + { |
| 85 | + if (treeNode.ParentNode != null) |
| 86 | + { |
| 87 | + |
| 88 | + var collection = AssociatedObject.sfTreeGrid.View.GetPropertyAccessProvider().GetValue(treeNode.ParentNode.Item, AssociatedObject.sfTreeGrid.ChildPropertyName) as IEnumerable; |
| 89 | + |
| 90 | + sourceCollection = GetSourceListCollection(collection); |
| 91 | + } |
| 92 | + |
| 93 | + else |
| 94 | + { |
| 95 | + sourceCollection = GetSourceListCollection(AssociatedObject.sfTreeGrid.View.SourceCollection); |
| 96 | + } |
| 97 | + itemIndex = sourceCollection.IndexOf(data); |
| 98 | + |
| 99 | + if (dropPosition == "DropBelow") |
| 100 | + { |
| 101 | + itemIndex += 1; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + else if (dropPosition == "DropAsChild") |
| 106 | + { |
| 107 | + var collection = AssociatedObject.sfTreeGrid.View.GetPropertyAccessProvider().GetValue(data, AssociatedObject.sfTreeGrid.ChildPropertyName) as IEnumerable; |
| 108 | + |
| 109 | + sourceCollection = GetSourceListCollection(collection); |
| 110 | + |
| 111 | + if (sourceCollection == null) |
| 112 | + { |
| 113 | + var list = data.GetType().GetProperty(AssociatedObject.sfTreeGrid.ChildPropertyName).PropertyType.CreateNew() as IList; |
| 114 | + |
| 115 | + if (list != null) |
| 116 | + { |
| 117 | + AssociatedObject.sfTreeGrid.View.GetPropertyAccessProvider().SetValue(treeNode.Item, AssociatedObject.sfTreeGrid.ChildPropertyName, list); |
| 118 | + sourceCollection = list; |
| 119 | + } |
| 120 | + } |
| 121 | + itemIndex = sourceCollection.Count; |
| 122 | + } |
| 123 | + sourceCollection.Insert(itemIndex, newItem); |
| 124 | + AssociatedObject.sfTreeGrid.SelectionController.ResumeUpdates(); |
| 125 | + (AssociatedObject.sfTreeGrid.SelectionController as TreeGridRowSelectionController).RefreshSelection(); |
| 126 | + e.Handled = true; |
| 127 | + } |
| 128 | + } |
| 129 | + (AssociatedObject.treeview.ItemsSource as ObservableCollection<EmployeeInfo>).Remove(record as EmployeeInfo); |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +In `TreeViewAdv`, you need to wire the `Drop` event, |
| 135 | + |
| 136 | +``` c# |
| 137 | +private void Treeview_Drop(object sender, DragEventArgs e) |
| 138 | +{ |
| 139 | + ObservableCollection<TreeNode> treeNodes = new ObservableCollection<TreeNode>(); |
| 140 | + |
| 141 | + if (e.Data.GetDataPresent("Nodes")) |
| 142 | + treeNodes = e.Data.GetData("Nodes") as ObservableCollection<TreeNode>; |
| 143 | + |
| 144 | + EmployeeInfo item = new EmployeeInfo(); |
| 145 | + |
| 146 | + if (treeNodes.Count == 0 || treeNodes == null) |
| 147 | + return; |
| 148 | + |
| 149 | + foreach (var node in treeNodes) |
| 150 | + { |
| 151 | + (AssociatedObject.sfTreeGrid.ItemsSource as ObservableCollection<EmployeeInfo>).Remove(node.Item as EmployeeInfo); |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
0 commit comments