Skip to content

Commit 8d7f85e

Browse files
authored
Merge pull request #1 from SyncfusionExamples/ES-975464
ES-975464 - Resolve the ReadMe file length issue in this sample repository
2 parents be5cb16 + 824031a commit 8d7f85e

File tree

2 files changed

+154
-2
lines changed

2 files changed

+154
-2
lines changed

DragAndDrop.png

80.9 KB
Loading

README.md

Lines changed: 154 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,154 @@
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
1+
# How to Drag and Drop Rows between WPF TreeGrid and WPF TreeViewAdv?
2+
3+
This example illustrates how to drag and drop rows in between [WPF TreeGrid](https://www.syncfusion.com/wpf-controls/treegrid) (SfTreeGrid) and [WPF TreeViewAdv](https://help.syncfusion.com/wpf/classic/treeview/overview).
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+
55+
var data = treeNode.Item;
56+
AssociatedObject.sfTreeGrid.SelectionController.SuspendUpdates();
57+
var itemIndex = -1;
58+
59+
TreeNode parentNode = null;
60+
61+
if (dropPosition == "DropBelow" || dropPosition == "DropAbove")
62+
{
63+
parentNode = treeNode.ParentNode;
64+
65+
if (parentNode == null)
66+
newItem = new EmployeeInfo() { FirstName = record.FirstName, LastName = record.LastName, ID = record.ID, Salary = record.Salary, Title = record.Title, ReportsTo = -1 };
67+
else
68+
{
69+
var parent = parentNode.Item as EmployeeInfo;
70+
newItem = new EmployeeInfo() { FirstName = record.FirstName, LastName = record.LastName, ID = record.ID, Salary = record.Salary, Title = record.Title, ReportsTo = parent.ID };
71+
}
72+
}
73+
else if (dropPosition == "DropAsChild")
74+
{
75+
if (!treeNode.IsExpanded)
76+
AssociatedObject.sfTreeGrid.ExpandNode(treeNode);
77+
78+
parentNode = treeNode;
79+
var parent = parentNode.Item as EmployeeInfo;
80+
newItem = new EmployeeInfo() { FirstName = record.FirstName, LastName = record.LastName, ID = record.ID, Salary = record.Salary, Title = record.Title, ReportsTo = parent.ID };
81+
82+
}
83+
IList sourceCollection = null;
84+
85+
if (dropPosition == "DropBelow" || dropPosition == "DropAbove")
86+
{
87+
if (treeNode.ParentNode != null)
88+
{
89+
var collection = AssociatedObject.sfTreeGrid.View.GetPropertyAccessProvider().GetValue(treeNode.ParentNode.Item, AssociatedObject.sfTreeGrid.ChildPropertyName) as IEnumerable;
90+
sourceCollection = GetSourceListCollection(collection);
91+
}
92+
else
93+
{
94+
sourceCollection = GetSourceListCollection(AssociatedObject.sfTreeGrid.View.SourceCollection);
95+
}
96+
itemIndex = sourceCollection.IndexOf(data);
97+
98+
if (dropPosition == "DropBelow")
99+
{
100+
itemIndex += 1;
101+
}
102+
}
103+
else if (dropPosition == "DropAsChild")
104+
{
105+
var collection = AssociatedObject.sfTreeGrid.View.GetPropertyAccessProvider().GetValue(data, AssociatedObject.sfTreeGrid.ChildPropertyName) as IEnumerable;
106+
107+
sourceCollection = GetSourceListCollection(collection);
108+
109+
if (sourceCollection == null)
110+
{
111+
var list = data.GetType().GetProperty(AssociatedObject.sfTreeGrid.ChildPropertyName).PropertyType.CreateNew() as IList;
112+
113+
if (list != null)
114+
{
115+
AssociatedObject.sfTreeGrid.View.GetPropertyAccessProvider().SetValue(treeNode.Item, AssociatedObject.sfTreeGrid.ChildPropertyName, list);
116+
sourceCollection = list;
117+
}
118+
}
119+
itemIndex = sourceCollection.Count;
120+
}
121+
sourceCollection.Insert(itemIndex, newItem);
122+
AssociatedObject.sfTreeGrid.SelectionController.ResumeUpdates();
123+
(AssociatedObject.sfTreeGrid.SelectionController as TreeGridRowSelectionController).RefreshSelection();
124+
e.Handled = true;
125+
}
126+
}
127+
(AssociatedObject.treeview.ItemsSource as ObservableCollection<EmployeeInfo>).Remove(record as EmployeeInfo);
128+
}
129+
}
130+
```
131+
132+
In TreeViewAdv, you need to wire the Drop event,
133+
134+
``` c#
135+
private void Treeview_Drop(object sender, DragEventArgs e)
136+
{
137+
ObservableCollection<TreeNode> treeNodes = new ObservableCollection<TreeNode>();
138+
139+
if (e.Data.GetDataPresent("Nodes"))
140+
treeNodes = e.Data.GetData("Nodes") as ObservableCollection<TreeNode>;
141+
142+
EmployeeInfo item = new EmployeeInfo();
143+
144+
if (treeNodes.Count == 0 || treeNodes == null)
145+
return;
146+
147+
foreach (var node in treeNodes)
148+
{
149+
(AssociatedObject.sfTreeGrid.ItemsSource as ObservableCollection<EmployeeInfo>).Remove(node.Item as EmployeeInfo);
150+
}
151+
}
152+
```
153+
154+
![Drag and drop between TreeGrid and TreeViewAdv](DragAndDrop.png)

0 commit comments

Comments
 (0)