|
| 1 | +using Syncfusion.Data.Extensions; |
| 2 | +using Syncfusion.UI.Xaml.Grid; |
| 3 | +using Syncfusion.UI.Xaml.TreeGrid; |
| 4 | +using System; |
| 5 | +using System.Collections; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Collections.ObjectModel; |
| 8 | +using System.Linq; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using System.Windows.Interactivity; |
| 12 | + |
| 13 | +namespace DragDropBetweenDataGridTreeGrid |
| 14 | +{ |
| 15 | + public class DragDropBehavior:Behavior<MainWindow> |
| 16 | + { |
| 17 | + |
| 18 | + protected override void OnAttached() |
| 19 | + { |
| 20 | + base.OnAttached(); |
| 21 | + AssociatedObject.sfDataGrid.RowDragDropController.Drop += sfDataGrid_Drop; |
| 22 | + AssociatedObject.sfTreeGrid.RowDragDropController.Drop += sfTreeGrid_Drop; |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Customized TreeGrid Drop event. |
| 27 | + /// </summary> |
| 28 | + /// <param name="sender"></param> |
| 29 | + /// <param name="e"></param> |
| 30 | + private void sfTreeGrid_Drop(object sender, TreeGridRowDropEventArgs e) |
| 31 | + { |
| 32 | + if (e.IsFromOutSideSource) |
| 33 | + { |
| 34 | + |
| 35 | + var draggingRecord = e.Data.GetData("Records") as ObservableCollection<object>; |
| 36 | + |
| 37 | + var record = draggingRecord[0] as EmployeeInfo; |
| 38 | + |
| 39 | + var dropPosition = e.DropPosition.ToString(); |
| 40 | + |
| 41 | + var newItem = new EmployeeInfo(); |
| 42 | + |
| 43 | + var rowIndex =AssociatedObject.sfTreeGrid.ResolveToRowIndex(e.TargetNode.Item); |
| 44 | + |
| 45 | + if (dropPosition != "None" && rowIndex != -1) |
| 46 | + { |
| 47 | + if (AssociatedObject.sfTreeGrid.View is TreeGridSelfRelationalView) |
| 48 | + { |
| 49 | + var treeNode = e.TargetNode; |
| 50 | + if (treeNode == null) |
| 51 | + return; |
| 52 | + |
| 53 | + var data = treeNode.Item; |
| 54 | + |
| 55 | + AssociatedObject.sfTreeGrid.SelectionController.SuspendUpdates(); |
| 56 | + |
| 57 | + var dropIndex = -1; |
| 58 | + |
| 59 | + TreeNode parentNode = null; |
| 60 | + |
| 61 | + if (dropPosition == "DropBelow" || dropPosition == "DropAbove") |
| 62 | + { |
| 63 | + parentNode = treeNode.ParentNode; |
| 64 | + if (parentNode == null) |
| 65 | + { |
| 66 | + var treeNodeItem = treeNode.Item as EmployeeInfo; |
| 67 | + newItem = new EmployeeInfo() { FirstName = record.FirstName, LastName = record.LastName, ID = record.ID, Salary = record.Salary, Title = record.Title, ReportsTo = treeNodeItem.ReportsTo }; |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + var parentNodeItems = parentNode.Item as EmployeeInfo; |
| 72 | + newItem = new EmployeeInfo() { FirstName = record.FirstName, LastName = record.LastName, ID = record.ID, Salary = record.Salary, Title = record.Title, ReportsTo = parentNodeItems.ID }; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + else if (dropPosition == "DropAsChild") |
| 77 | + { |
| 78 | + |
| 79 | + if (!treeNode.IsExpanded) |
| 80 | + AssociatedObject.sfTreeGrid.ExpandNode(treeNode); |
| 81 | + parentNode = treeNode; |
| 82 | + var parentNodeItems = parentNode.Item as EmployeeInfo; |
| 83 | + newItem = new EmployeeInfo() { FirstName = record.FirstName, LastName = record.LastName, ID = record.ID, Salary = record.Salary, Title = record.Title, ReportsTo = parentNodeItems.ID }; |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + IList sourceCollection = null; |
| 88 | + |
| 89 | + |
| 90 | + if (dropPosition == "DropBelow" || dropPosition == "DropAbove") |
| 91 | + { |
| 92 | + |
| 93 | + if (treeNode.ParentNode != null) |
| 94 | + { |
| 95 | + var collection = AssociatedObject.sfTreeGrid.View.GetPropertyAccessProvider().GetValue(treeNode.ParentNode.Item, AssociatedObject.sfTreeGrid.ChildPropertyName) as IEnumerable; |
| 96 | + sourceCollection = GetSourceListCollection(collection); |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + sourceCollection = GetSourceListCollection(AssociatedObject.sfTreeGrid.View.SourceCollection); |
| 101 | + } |
| 102 | + dropIndex = sourceCollection.IndexOf(data); |
| 103 | + |
| 104 | + if (dropPosition == "DropBelow") |
| 105 | + { |
| 106 | + dropIndex += 1; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + else if (dropPosition == "DropAsChild") |
| 111 | + { |
| 112 | + var collection = AssociatedObject.sfTreeGrid.View.GetPropertyAccessProvider().GetValue(data, AssociatedObject.sfTreeGrid.ChildPropertyName) as IEnumerable; |
| 113 | + |
| 114 | + sourceCollection = GetSourceListCollection(collection); |
| 115 | + |
| 116 | + if (sourceCollection == null) |
| 117 | + { |
| 118 | + var list = data.GetType().GetProperty(AssociatedObject.sfTreeGrid.ChildPropertyName).PropertyType.CreateNew() as IList; |
| 119 | + |
| 120 | + if (list != null) |
| 121 | + { |
| 122 | + AssociatedObject.sfTreeGrid.View.GetPropertyAccessProvider().SetValue(treeNode.Item, AssociatedObject.sfTreeGrid.ChildPropertyName, list); |
| 123 | + sourceCollection = list; |
| 124 | + } |
| 125 | + } |
| 126 | + dropIndex = sourceCollection.Count; |
| 127 | + } |
| 128 | + sourceCollection.Insert(dropIndex, newItem); |
| 129 | + |
| 130 | + AssociatedObject.sfTreeGrid.SelectionController.ResumeUpdates(); |
| 131 | + (AssociatedObject.sfTreeGrid.SelectionController as TreeGridRowSelectionController).RefreshSelection(); |
| 132 | + e.Handled = true; |
| 133 | + } |
| 134 | + } |
| 135 | + AssociatedObject.sfDataGrid.View.Remove(record); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + /// <summary> |
| 142 | + /// Gets the source collection of TreeGrid |
| 143 | + /// </summary> |
| 144 | + /// <param name="collection"></param> |
| 145 | + /// <returns></returns> |
| 146 | + private IList GetSourceListCollection(IEnumerable collection) |
| 147 | + { |
| 148 | + IList list = null; |
| 149 | + if (collection == null) |
| 150 | + collection = AssociatedObject.sfTreeGrid.View.SourceCollection; |
| 151 | + if ((collection as IList) != null) |
| 152 | + { |
| 153 | + list = collection as IList; |
| 154 | + } |
| 155 | + return list; |
| 156 | + } |
| 157 | + |
| 158 | + /// <summary> |
| 159 | + /// Customize the Drop event.restrict the certain record and Drop position from drop. |
| 160 | + /// </summary> |
| 161 | + /// <param name="sender"></param> |
| 162 | + /// <param name="e"></param> |
| 163 | + private void sfDataGrid_Drop(object sender, GridRowDropEventArgs e) |
| 164 | + { |
| 165 | + |
| 166 | + if (e.IsFromOutSideSource) |
| 167 | + { |
| 168 | + var draggingRecord = e.Data.GetData("Nodes") as ObservableCollection<TreeNode>; |
| 169 | + |
| 170 | + var record = draggingRecord[0].Item as EmployeeInfo; |
| 171 | + |
| 172 | + int dropIndex = (int)e.TargetRecord; |
| 173 | + |
| 174 | + |
| 175 | + var dropPosition = e.DropPosition.ToString(); |
| 176 | + |
| 177 | + if (record.Title == "Manager") |
| 178 | + { |
| 179 | + e.Handled = true; |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | + IList collection = null; |
| 185 | + |
| 186 | + collection = AssociatedObject.sfDataGrid.View.SourceCollection as IList; |
| 187 | + if (dropPosition == "DropAbove") |
| 188 | + { |
| 189 | + dropIndex--; |
| 190 | + collection.Insert(dropIndex, record); |
| 191 | + |
| 192 | + } |
| 193 | + else |
| 194 | + { |
| 195 | + dropIndex++; |
| 196 | + collection.Insert(dropIndex, record); |
| 197 | + |
| 198 | + } |
| 199 | + AssociatedObject.sfTreeGrid.View.Remove(record); |
| 200 | + e.Handled = true; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + protected override void OnDetaching() |
| 205 | + { |
| 206 | + base.OnDetaching(); |
| 207 | + |
| 208 | + AssociatedObject.sfDataGrid.RowDragDropController.Drop -= sfDataGrid_Drop; |
| 209 | + AssociatedObject.sfTreeGrid.RowDragDropController.Drop -= sfTreeGrid_Drop; |
| 210 | + } |
| 211 | + |
| 212 | + } |
| 213 | +} |
0 commit comments