1- using Syncfusion . Data . Extensions ;
1+ using Microsoft . Xaml . Behaviors ;
2+ using Syncfusion . Data . Extensions ;
23using Syncfusion . UI . Xaml . Grid ;
34using Syncfusion . UI . Xaml . TreeGrid ;
45using System ;
89using System . Linq ;
910using System . Text ;
1011using System . Threading . Tasks ;
11- using System . Windows . Interactivity ;
1212
1313namespace DragDropBetweenDataGridTreeGrid
1414{
@@ -21,7 +21,7 @@ protected override void OnAttached()
2121 AssociatedObject . sfDataGrid . RowDragDropController . Drop += sfDataGrid_Drop ;
2222 AssociatedObject . sfTreeGrid . RowDragDropController . Drop += sfTreeGrid_Drop ;
2323 }
24-
24+
2525 /// <summary>
2626 /// Customized TreeGrid Drop event.
2727 /// </summary>
@@ -31,75 +31,56 @@ private void sfTreeGrid_Drop(object sender, TreeGridRowDropEventArgs e)
3131 {
3232 if ( e . IsFromOutSideSource )
3333 {
34-
34+ //Getting the dragging record from e.Data
3535 var draggingRecord = e . Data . GetData ( "Records" ) as ObservableCollection < object > ;
36-
36+
3737 var record = draggingRecord [ 0 ] as EmployeeInfo ;
38-
38+
39+ //Getting the Drop position
3940 var dropPosition = e . DropPosition . ToString ( ) ;
40-
41+
4142 var newItem = new EmployeeInfo ( ) ;
4243
43- var rowIndex = AssociatedObject . sfTreeGrid . ResolveToRowIndex ( e . TargetNode . Item ) ;
44-
44+ //Getting the Target node index
45+ var rowIndex = AssociatedObject . sfTreeGrid . ResolveToRowIndex ( e . TargetNode . Item ) ;
46+
4547 if ( dropPosition != "None" && rowIndex != - 1 )
4648 {
4749 if ( AssociatedObject . sfTreeGrid . View is TreeGridSelfRelationalView )
4850 {
4951 var treeNode = e . TargetNode ;
5052 if ( treeNode == null )
5153 return ;
52-
53- var data = treeNode . Item ;
5454
55- AssociatedObject . sfTreeGrid . SelectionController . SuspendUpdates ( ) ;
56-
55+ var targetData = treeNode . Item ;
56+
5757 var dropIndex = - 1 ;
5858
5959 TreeNode parentNode = null ;
6060
61+ IList sourceCollection = null ;
62+
6163 if ( dropPosition == "DropBelow" || dropPosition == "DropAbove" )
6264 {
65+ //Create the new item based on ParentNode and Its ChildPropertyName then get its source collection
6366 parentNode = treeNode . ParentNode ;
6467 if ( parentNode == null )
6568 {
6669 var treeNodeItem = treeNode . Item as EmployeeInfo ;
6770 newItem = new EmployeeInfo ( ) { FirstName = record . FirstName , LastName = record . LastName , ID = record . ID , Salary = record . Salary , Title = record . Title , ReportsTo = treeNodeItem . ReportsTo } ;
71+ sourceCollection = GetSourceListCollection ( AssociatedObject . sfTreeGrid . View . SourceCollection ) ;
6872 }
6973 else
7074 {
7175 var parentNodeItems = parentNode . Item as EmployeeInfo ;
7276 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- {
9577 var collection = AssociatedObject . sfTreeGrid . View . GetPropertyAccessProvider ( ) . GetValue ( treeNode . ParentNode . Item , AssociatedObject . sfTreeGrid . ChildPropertyName ) as IEnumerable ;
9678 sourceCollection = GetSourceListCollection ( collection ) ;
9779 }
98- else
99- {
100- sourceCollection = GetSourceListCollection ( AssociatedObject . sfTreeGrid . View . SourceCollection ) ;
101- }
102- dropIndex = sourceCollection . IndexOf ( data ) ;
80+
81+ //Get the drop index based on DropPosition
82+
83+ dropIndex = sourceCollection . IndexOf ( targetData ) ;
10384
10485 if ( dropPosition == "DropBelow" )
10586 {
@@ -109,13 +90,22 @@ private void sfTreeGrid_Drop(object sender, TreeGridRowDropEventArgs e)
10990
11091 else if ( dropPosition == "DropAsChild" )
11192 {
112- var collection = AssociatedObject . sfTreeGrid . View . GetPropertyAccessProvider ( ) . GetValue ( data , AssociatedObject . sfTreeGrid . ChildPropertyName ) as IEnumerable ;
93+ //Expand the parent node if it is not expanded
94+ if ( ! treeNode . IsExpanded )
95+ AssociatedObject . sfTreeGrid . ExpandNode ( treeNode ) ;
96+
97+ //Create the new item based on ParentNode and Its ChildPropertyName then get its source collection
98+ parentNode = treeNode ;
99+ var parentNodeItems = parentNode . Item as EmployeeInfo ;
100+ newItem = new EmployeeInfo ( ) { FirstName = record . FirstName , LastName = record . LastName , ID = record . ID , Salary = record . Salary , Title = record . Title , ReportsTo = parentNodeItems . ID } ;
101+
102+ var collection = AssociatedObject . sfTreeGrid . View . GetPropertyAccessProvider ( ) . GetValue ( targetData , AssociatedObject . sfTreeGrid . ChildPropertyName ) as IEnumerable ;
113103
114104 sourceCollection = GetSourceListCollection ( collection ) ;
115105
116106 if ( sourceCollection == null )
117107 {
118- var list = data . GetType ( ) . GetProperty ( AssociatedObject . sfTreeGrid . ChildPropertyName ) . PropertyType . CreateNew ( ) as IList ;
108+ var list = targetData . GetType ( ) . GetProperty ( AssociatedObject . sfTreeGrid . ChildPropertyName ) . PropertyType . CreateNew ( ) as IList ;
119109
120110 if ( list != null )
121111 {
@@ -125,13 +115,15 @@ private void sfTreeGrid_Drop(object sender, TreeGridRowDropEventArgs e)
125115 }
126116 dropIndex = sourceCollection . Count ;
127117 }
118+
119+ //Insert the new item to the source collection based on DropIndex
128120 sourceCollection . Insert ( dropIndex , newItem ) ;
129121
130- AssociatedObject . sfTreeGrid . SelectionController . ResumeUpdates ( ) ;
131- ( AssociatedObject . sfTreeGrid . SelectionController as TreeGridRowSelectionController ) . RefreshSelection ( ) ;
132122 e . Handled = true ;
133123 }
134124 }
125+
126+ //Remove the record from the source collection
135127 AssociatedObject . sfDataGrid . View . Remove ( record ) ;
136128 }
137129 }
@@ -165,25 +157,31 @@ private void sfDataGrid_Drop(object sender, GridRowDropEventArgs e)
165157
166158 if ( e . IsFromOutSideSource )
167159 {
160+ //Getting the dragging record from e.Data
168161 var draggingRecord = e . Data . GetData ( "Nodes" ) as ObservableCollection < TreeNode > ;
169162
170163 var record = draggingRecord [ 0 ] . Item as EmployeeInfo ;
171164
165+ //Getting the Drop Index
172166 int dropIndex = ( int ) e . TargetRecord ;
173167
174-
168+ //Getting the Drop position
175169 var dropPosition = e . DropPosition . ToString ( ) ;
176170
171+ //Restrict the certain record from drop
177172 if ( record . Title == "Manager" )
178173 {
179174 e . Handled = true ;
180175 return ;
181176 }
182-
183-
177+
178+
179+ //Get the Source Collection
184180 IList collection = null ;
185181
186182 collection = AssociatedObject . sfDataGrid . View . SourceCollection as IList ;
183+
184+ //Insert the new item to the source collection based on DropIndex
187185 if ( dropPosition == "DropAbove" )
188186 {
189187 dropIndex -- ;
@@ -196,6 +194,8 @@ private void sfDataGrid_Drop(object sender, GridRowDropEventArgs e)
196194 collection . Insert ( dropIndex , record ) ;
197195
198196 }
197+
198+ //Remove the record from the source collection
199199 AssociatedObject . sfTreeGrid . View . Remove ( record ) ;
200200 e . Handled = true ;
201201 }
0 commit comments