|
3 | 3 | ## About the sample |
4 | 4 | This example illustrates how to drag and drop items in WinUI TreeView (SfTreeView)? |
5 | 5 |
|
6 | | -[WinUI TreeView](https://www.syncfusion.com/winui-controls/treeview) (SfTreeView) allows drag and drop the items within and between controls by setting the [CanDrag](https://docs.microsoft.com/en-us/windows/winui/api/microsoft.ui.xaml.uielement.candrag?view=winui-3.0) and [AllowDrop](https://docs.microsoft.com/en-us/windows/winui/api/microsoft.ui.xaml.uielement.allowdrop?view=winui-3.0) properties to true. It is also possible to drag and drop the items between Treeview and other controls such as **ListView** and **SfDataGrid**. |
| 6 | +[WinUI TreeView](https://www.syncfusion.com/winui-controls/treeview) (SfTreeView) allows drag and drop the items within and between controls by setting the CanDrag and AllowDrop properties to true. It is also possible to drag and drop the items between Treeview and other controls such as ListView and WPF DataGrid (SfDataGrid). |
7 | 7 |
|
8 | 8 | ```XML |
9 | | - |
10 | | -<treeView:SfTreeView x:Name="treeView" |
11 | | - Width="400" |
12 | | - Height="500" |
13 | | - CanDrag="True" |
14 | | - AllowDrop="True" |
15 | | - AutoExpandMode="AllNodes" |
16 | | - ChildPropertyName="Childs" |
17 | | - BorderBrush="LightGray" |
18 | | - IsAnimationEnabled="True" |
19 | | - BorderThickness="1" |
20 | | - FullRowSelect="True" |
21 | | - ItemsSource="{Binding Nodes1}"> |
22 | | - <treeView:SfTreeView.ItemTemplate> |
23 | | - <DataTemplate> |
24 | | - <StackPanel Orientation="Horizontal"> |
25 | | - <ContentPresenter Width="20" |
26 | | - Height="20" |
27 | | - HorizontalAlignment="Stretch" |
28 | | - VerticalAlignment="Center" |
29 | | - ContentTemplate="{Binding ImageTemplate}" /> |
30 | | - <TextBlock Margin="5" |
31 | | - VerticalAlignment="Center" |
32 | | - Text="{Binding Header}" /> |
33 | | - </StackPanel> |
34 | | - </DataTemplate> |
35 | | - </treeView:SfTreeView.ItemTemplate> |
36 | | -</treeView:SfTreeView> |
37 | | - |
| 9 | +<treeView:SfTreeView x:Name="treeView" |
| 10 | + Width="400" |
| 11 | + Height="500" |
| 12 | + CanDrag="True" |
| 13 | + AllowDrop="True" |
| 14 | + AutoExpandMode="AllNodes" |
| 15 | + ChildPropertyName="Childs" |
| 16 | + BorderBrush="LightGray" |
| 17 | + IsAnimationEnabled="True" |
| 18 | + BorderThickness="1" |
| 19 | + FullRowSelect="True" |
| 20 | + ItemsSource="{Binding Nodes1}"> |
| 21 | + <treeView:SfTreeView.ItemTemplate> |
| 22 | + <DataTemplate> |
| 23 | + <StackPanel Orientation="Horizontal"> |
| 24 | + <ContentPresenter Width="20" |
| 25 | + Height="20" |
| 26 | + HorizontalAlignment="Stretch" |
| 27 | + VerticalAlignment="Center" |
| 28 | + ContentTemplate="{Binding ImageTemplate}" /> |
| 29 | + <TextBlock Margin="5" |
| 30 | + VerticalAlignment="Center" |
| 31 | + Text="{Binding Header}" /> |
| 32 | + </StackPanel> |
| 33 | + </DataTemplate> |
| 34 | + </treeView:SfTreeView.ItemTemplate> |
| 35 | + </treeView:SfTreeView> |
38 | 36 | ``` |
39 | 37 |
|
40 | 38 |  |
@@ -99,59 +97,61 @@ treeView.ItemDropping += OnFirstTreeViewItemDropping; |
99 | 97 | treeView1.ItemDropping += OnItemDropping; |
100 | 98 | treeView1.ItemDropped += OnItemDropped; |
101 | 99 |
|
102 | | -private void OnItemDropped(object sender, Syncfusion.UI.Xaml.TreeView.TreeViewItemDroppedEventArgs e) |
103 | | -{ |
104 | | - var parentNode = e.TargetNode.ParentNode; |
105 | | - var collection = parentNode.ChildNodes; |
106 | | - var record = e.DraggingNodes[0].Content as Folder; |
107 | | - int count = 0; |
108 | | - foreach (var child in parentNode.ChildNodes) |
109 | | - { |
110 | | - var childNode = child.Content as Folder; |
111 | | - if (childNode.FileName == record.FileName) |
112 | | - { |
113 | | - count++; |
114 | | - if (count > 1) |
115 | | - { |
116 | | - // Remove dropped node if the parent has the same node in it |
117 | | - collection.Remove(child); |
118 | | - return; |
119 | | - } |
120 | | - |
121 | | - } |
122 | | - } |
123 | | -} |
124 | | - |
125 | | -private void OnItemDropping(object sender, Syncfusion.UI.Xaml.TreeView.TreeViewItemDroppingEventArgs e) |
126 | | -{ |
127 | | - //Restrict the dropping for drop position as above |
128 | | - if (e.DropPosition == Syncfusion.UI.Xaml.TreeView.DropPosition.DropAbove) |
129 | | - e.Handled = true; |
130 | | - |
131 | | - //Restrict the dropping on certain nodes |
132 | | - var record = e.TargetNode.Content as Folder; |
133 | | - if (record.FileName == "Documents") |
134 | | - e.Handled = true; |
135 | | -} |
136 | | - |
137 | | -private void OnItemDragStarting(object sender, Syncfusion.UI.Xaml.TreeView.TreeViewItemDragStartingEventArgs e) |
138 | | -{ |
139 | | - //Restrict the dragging for certain node |
140 | | - var record = e.DraggingNodes[0].Content as Folder; |
141 | | - if (record.FileName == "Downloads") |
142 | | - e.Cancel = true; |
143 | | -} |
144 | | - |
145 | | -private void OnFirstTreeViewItemDropping(object sender, Syncfusion.UI.Xaml.TreeView.TreeViewItemDroppingEventArgs e) |
146 | | -{ |
147 | | - //Restrict the dropping in first treeview |
148 | | - e.Handled = true; |
149 | | -} |
| 100 | +treeView.ItemDragStarting += OnItemDragStarting; |
| 101 | + treeView.ItemDropping += OnFirstTreeViewItemDropping; |
| 102 | + treeView1.ItemDropping += OnItemDropping; |
| 103 | + treeView1.ItemDropped += OnItemDropped; |
| 104 | + |
| 105 | + private void OnItemDropped(object sender, TreeViewItemDroppedEventArgs e) |
| 106 | + { |
| 107 | + var parentNode = e.TargetNode.ParentNode; |
| 108 | + var collection = parentNode.ChildNodes; |
| 109 | + var record = e.DraggingNodes[0].Content as Folder; |
| 110 | + int count = 0; |
| 111 | + foreach (var child in parentNode.ChildNodes) |
| 112 | + { |
| 113 | + var childNode = child.Content as Folder; |
| 114 | + if (childNode.FileName == record.FileName) |
| 115 | + { |
| 116 | + count++; |
| 117 | + if (count > 1) |
| 118 | + { |
| 119 | + // Remove dropped node if the parent has the same node in it |
| 120 | + collection.Remove(child); |
| 121 | + return; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private void OnItemDropping(object sender,TreeViewItemDroppingEventArgs e) |
| 128 | + { |
| 129 | + //Restrict the dropping for drop position as above |
| 130 | + if (e.DropPosition == Syncfusion.UI.Xaml.TreeView.DropPosition.DropAbove) |
| 131 | + e.Handled = true; |
| 132 | + |
| 133 | + //Restrict the dropping on certain nodes |
| 134 | + var record = e.TargetNode.Content as Folder; |
| 135 | + if (record.FileName == "Documents") |
| 136 | + e.Handled = true; |
| 137 | + } |
| 138 | + |
| 139 | + private void OnItemDragStarting(object sender, TreeViewItemDragStartingEventArgs e) |
| 140 | + { |
| 141 | + //Restrict the dragging for certain node |
| 142 | + var record = e.DraggingNodes[0].Content as Folder; |
| 143 | + if (record.FileName == "Downloads") |
| 144 | + e.Cancel = true; |
| 145 | + } |
| 146 | + |
| 147 | + private void OnFirstTreeViewItemDropping(object sender, TreeViewItemDroppingEventArgs e) |
| 148 | + { |
| 149 | + //Restrict the dropping in first treeview |
| 150 | + e.Handled = true; |
| 151 | + } |
150 | 152 |
|
151 | 153 | ``` |
152 | 154 |
|
153 | 155 |  |
154 | 156 |
|
155 | 157 | Take a moment to peruse the [WinUI TreeView - Drag and Drop](https://help.syncfusion.com/winui/treeview/drag-and-drop) documentation, to learn more about drag and drop with code examples. |
156 | | - |
157 | | - |
0 commit comments