Skip to content

Commit c14fbf9

Browse files
authored
Merge pull request #2 from SyncfusionExamples/ES-975464
ES-975464 - Resolve the ReadMe file length issue in this sample repository
2 parents 9f03d58 + 1c2fc29 commit c14fbf9

File tree

1 file changed

+202
-2
lines changed

1 file changed

+202
-2
lines changed

README.md

Lines changed: 202 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,202 @@
1-
# How to drag and drop rows in wpf and uwp between listview and treegrid?
2-
This example illustrates how to drag and drop rows in wpf and uwp between listview and treegrid.
1+
# How to Drag and Drop Rows in WPF / UWP between ListView and TreeGrid?
2+
3+
This example illustrates how to drag and drop rows between [WPF TreeGrid](https://www.syncfusion.com/wpf-controls/treegrid) / [UWP TreeGrid](https://www.syncfusion.com/uwp-ui-controls/treegrid) (SfTreeGrid) and listview.
4+
5+
You can drag the item from ListView and drop into TreeGrid by overriding the [GetDropPosition](https://help.syncfusion.com/cr/uwp/Syncfusion.UI.Xaml.TreeGrid.TreeGridRowDragDropController.html#Syncfusion_UI_Xaml_TreeGrid_TreeGridRowDragDropController_GetDropPosition_Windows_UI_Xaml_DragEventArgs_Syncfusion_UI_Xaml_ScrollAxis_RowColumnIndex_), [ProcessOnDragOver](https://help.syncfusion.com/cr/uwp/Syncfusion.UI.Xaml.TreeGrid.TreeGridRowDragDropController.html#Syncfusion_UI_Xaml_TreeGrid_TreeGridRowDragDropController_ProcessOnDragOver_Windows_UI_Xaml_DragEventArgs_Syncfusion_UI_Xaml_ScrollAxis_RowColumnIndex_), and [ProcessOnDrop](https://help.syncfusion.com/cr/uwp/Syncfusion.UI.Xaml.TreeGrid.TreeGridRowDragDropController.html#Syncfusion_UI_Xaml_TreeGrid_TreeGridRowDragDropController_ProcessOnDrop_Windows_UI_Xaml_DragEventArgs_Syncfusion_UI_Xaml_ScrollAxis_RowColumnIndex_) methods in the [TreeGridRowDragDropController](https://help.syncfusion.com/cr/uwp/Syncfusion.UI.Xaml.TreeGrid.TreeGridRowDragDropController.html) class.
6+
7+
In ListView, use the **DragItemsStarting** event and add dragged item.
8+
9+
``` csharp
10+
listView.CanDragItems = true;
11+
listView.DragItemsStarting += ListView_DragItemsStarting;
12+
listView.DragItemsCompleted += ListView_DragItemsCompleted;
13+
14+
private void ListView_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
15+
{
16+
e.Data.Properties.Add("DraggedItem", listView.SelectedItem);
17+
e.Data.SetText(StandardDataFormats.Text);
18+
}
19+
20+
private void ListView_DragItemsCompleted(ListViewBase sender, DragItemsCompletedEventArgs args)
21+
{
22+
foreach (var item in args.Items)
23+
{
24+
(listView.ItemsSource as ObservableCollection<PersonInfo>).Remove(item as PersonInfo);
25+
}
26+
}
27+
```
28+
29+
``` csharp
30+
public class TreeGridRowDragDropControllerExt : TreeGridRowDragDropController
31+
{
32+
33+
public TreeGridRowDragDropControllerExt(SfTreeGrid treeGrid) : base(treeGrid)
34+
{
35+
}
36+
37+
protected override void ProcessOnDrop(DragEventArgs args, RowColumnIndex rowColumnIndex)
38+
{
39+
this.TreeGrid.AutoScroller.AutoScrolling = AutoScrollOrientation.None;
40+
var dropPosition = GetDropPosition(args, rowColumnIndex);
41+
42+
if (dropPosition != DropPosition.None && rowColumnIndex.RowIndex != -1)
43+
{
44+
var draggedItem = GetDraggedItem(args);
45+
46+
if (this.TreeGrid.View is TreeGridNestedView)
47+
{
48+
var treeNode = this.TreeGrid.GetNodeAtRowIndex(rowColumnIndex.RowIndex);
49+
50+
if (treeNode == null)
51+
return;
52+
var data = treeNode.Item;
53+
TreeGrid.SelectionController.SuspendUpdates();
54+
var itemIndex = -1;
55+
56+
TreeNode parentNode = null;
57+
58+
if (dropPosition == DropPosition.DropBelow || dropPosition == DropPosition.DropAbove)
59+
parentNode = treeNode.ParentNode;
60+
else if (dropPosition == DropPosition.DropAsChild)
61+
{
62+
if (!treeNode.IsExpanded)
63+
TreeGrid.ExpandNode(treeNode);
64+
parentNode = treeNode;
65+
}
66+
IList sourceCollection = null;
67+
68+
if (dropPosition == DropPosition.DropBelow || dropPosition == DropPosition.DropAbove)
69+
{
70+
71+
if (treeNode.ParentNode != null)
72+
{
73+
var collection = TreeGrid.View.GetPropertyAccessProvider().GetValue(treeNode.ParentNode.Item, TreeGrid.ChildPropertyName) as IEnumerable;
74+
sourceCollection = GetSourceListCollection(collection);
75+
}
76+
else
77+
{
78+
sourceCollection = GetSourceListCollection(TreeGrid.View.SourceCollection);
79+
}
80+
81+
itemIndex = sourceCollection.IndexOf(data);
82+
83+
if (dropPosition == DropPosition.DropBelow)
84+
{
85+
itemIndex += 1;
86+
}
87+
}
88+
else if (dropPosition == DropPosition.DropAsChild)
89+
{
90+
var collection = TreeGrid.View.GetPropertyAccessProvider().GetValue(data, TreeGrid.ChildPropertyName) as IEnumerable;
91+
sourceCollection = GetSourceListCollection(collection);
92+
93+
if (sourceCollection == null)
94+
{
95+
var list = data.GetType().GetProperty(TreeGrid.ChildPropertyName).PropertyType.CreateNew() as IList;
96+
97+
if (list != null)
98+
{
99+
TreeGrid.View.GetPropertyAccessProvider().SetValue(treeNode.Item, TreeGrid.ChildPropertyName, list);
100+
sourceCollection = list;
101+
}
102+
}
103+
itemIndex = sourceCollection.Count;
104+
}
105+
sourceCollection.Insert(itemIndex, draggedItem);
106+
TreeGrid.SelectionController.ResumeUpdates();
107+
(TreeGrid.SelectionController as TreeGridRowSelectionController).RefreshSelection();
108+
}
109+
}
110+
CloseDragIndicators();
111+
}
112+
113+
protected override DropPosition GetDropPosition(DragEventArgs args, RowColumnIndex rowColumnIndex)
114+
{
115+
bool canDrop = true;
116+
var p = args.GetPosition(this.TreeGrid);
117+
var treeNode = this.TreeGrid.GetNodeAtRowIndex(rowColumnIndex.RowIndex);
118+
ScrollAxisRegion columnRegion = ScrollAxisRegion.Body;
119+
var treeGridPanel = TreeGrid.GetTreePanel();
120+
121+
if (treeGridPanel.FrozenColumns > 0)
122+
columnRegion = ScrollAxisRegion.Header;
123+
var rowRect = treeGridPanel.RangeToRect(ScrollAxisRegion.Body, columnRegion, rowColumnIndex, true, false);
124+
var node = treeNode;
125+
126+
if (!canDrop)
127+
return DropPosition.None;
128+
else if (p.Y > rowRect.Y + 15 && p.Y < rowRect.Y + 35)
129+
{
130+
return DropPosition.DropAsChild;
131+
}
132+
else if (p.Y < rowRect.Y + 15)
133+
{
134+
return DropPosition.DropAbove;
135+
}
136+
else if (p.Y > rowRect.Y + 35)
137+
{
138+
return DropPosition.DropBelow;
139+
}
140+
else
141+
return DropPosition.Default;
142+
}
143+
144+
protected override void ProcessOnDragOver(DragEventArgs args, RowColumnIndex rowColumnIndex)
145+
{
146+
autoExpandNode = null;
147+
var node = this.TreeGrid.GetNodeAtRowIndex(rowColumnIndex.RowIndex);
148+
var draggedItem = GetDraggedItem(args);
149+
var dropPosition = GetDropPosition(args, rowColumnIndex);
150+
151+
if (draggedItem == null)
152+
return;
153+
154+
if (dropPosition == DropPosition.None || dropPosition == DropPosition.Default)
155+
{
156+
CloseDragIndicators();
157+
args.AcceptedOperation = DataPackageOperation.None;
158+
args.DragUIOverride.Caption = "Can't drop here";
159+
return;
160+
}
161+
else if (dropPosition == DropPosition.DropAbove)
162+
{
163+
args.DragUIOverride.Caption = "Drop above";
164+
}
165+
else if (dropPosition == DropPosition.DropAsChild)
166+
{
167+
args.DragUIOverride.Caption = "Drop as child";
168+
}
169+
else
170+
{
171+
args.DragUIOverride.Caption = "Drop below";
172+
}
173+
args.AcceptedOperation = DataPackageOperation.Move;
174+
ShowDragIndicators(dropPosition, rowColumnIndex);
175+
args.Handled = true;
176+
}
177+
178+
private object GetDraggedItem(DragEventArgs dragEventArgs)
179+
{
180+
if (dragEventArgs.DataView.Properties.ContainsKey("DraggedItem"))
181+
return dragEventArgs.DataView.Properties["DraggedItem"];
182+
else
183+
return null;
184+
}
185+
186+
internal IList GetSourceListCollection(IEnumerable collection = null)
187+
{
188+
IList list = null;
189+
190+
if (collection == null)
191+
collection = TreeGrid.View.SourceCollection;
192+
193+
if ((collection as IList) != null)
194+
{
195+
list = collection as IList;
196+
}
197+
return list;
198+
}
199+
}
200+
```
201+
202+
After dropping in TreeGrid, the dragged item is added to TreeGrid based on the dropped position.

0 commit comments

Comments
 (0)