|
1 | | -# How-to-restrict-drag-and-drop-of-nodes-in-WPF-TreeViewAdv |
2 | | -This session explains how to restrict drag and drop of nodes in WPF TreeViewAdv. |
| 1 | +# How to restrict drag and drop of parent node into another parent node in WPF TreeViewAdv? |
3 | 2 |
|
4 | | -KB article - [How-to-restrict-drag-and-drop-of-nodes-in-WPF-TreeViewAdv](https://www.syncfusion.com/kb/11652/how-to-restrict-drag-and-drop-of-parent-node-into-another-parent-node-in-wpf-treeviewadv) |
| 3 | +This article will explain how to restrict drag and drop of a parent node into another parent node, where it will allow only reordering the child nodes in [WPF TreeViewAdv](https://help.syncfusion.com/wpf/classic/treeview/overview). This can be achieved by using [DragStart](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.Controls.TreeViewAdv.html#Syncfusion_Windows_Tools_Controls_TreeViewAdv_DragStart) and [DragEnd](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.Tools.Controls.TreeViewAdv.html#Syncfusion_Windows_Tools_Controls_TreeViewAdv_DragEnd) event. |
| 4 | + |
| 5 | +```csharp |
| 6 | +public class Model |
| 7 | +{ |
| 8 | + public Model() |
| 9 | + { |
| 10 | + SubItems = new ObservableCollection<Model>(); |
| 11 | + } |
| 12 | + |
| 13 | + public string Header { get; set; } |
| 14 | + public ObservableCollection<Model> SubItems { get; set; } |
| 15 | +} |
| 16 | +``` |
| 17 | + |
| 18 | +```csharp |
| 19 | +public class ViewModel |
| 20 | +{ |
| 21 | + public ViewModel() |
| 22 | + { |
| 23 | + TreeItems = new ObservableCollection<Model>(); |
| 24 | + PopulateData(); |
| 25 | + } |
| 26 | + |
| 27 | + public ObservableCollection<Model> TreeItems { get; set; } |
| 28 | + |
| 29 | + private void PopulateData() |
| 30 | + { |
| 31 | + Model root1 = new Model() { Header = "Root1" }; |
| 32 | + PopulateSubItems(root1); |
| 33 | + TreeItems.Add(root1); |
| 34 | + |
| 35 | + Model root2 = new Model() { Header = "Root2" }; |
| 36 | + PopulateSubItems(root2); |
| 37 | + TreeItems.Add(root2); |
| 38 | + |
| 39 | + Model root3 = new Model() { Header = "Root3" }; |
| 40 | + PopulateSubItems(root3); |
| 41 | + TreeItems.Add(root3); |
| 42 | + } |
| 43 | + |
| 44 | + private void PopulateSubItems(Model root) |
| 45 | + { |
| 46 | + Model subItem1 = new Model() { Header = "Item1" }; |
| 47 | + Model subItem2 = new Model() { Header = "Item2" }; |
| 48 | + Model subItem3 = new Model() { Header = "Item3" }; |
| 49 | + Model subItem4 = new Model() { Header = "Item4" }; |
| 50 | + |
| 51 | + root.SubItems.Add(subItem1); |
| 52 | + root.SubItems.Add(subItem2); |
| 53 | + root.SubItems.Add(subItem3); |
| 54 | + root.SubItems.Add(subItem4); |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +```xml |
| 60 | +<Grid> |
| 61 | + <syncfusion:TreeViewAdv Margin="10" |
| 62 | + x:Name="tree" |
| 63 | + AllowDragDrop="True" |
| 64 | + ItemsSource="{Binding TreeItems}"> |
| 65 | + <syncfusion:TreeViewAdv.ItemTemplate> |
| 66 | + <HierarchicalDataTemplate ItemsSource="{Binding SubItems}"> |
| 67 | + <TextBlock Text="{Binding Header}" /> |
| 68 | + </HierarchicalDataTemplate> |
| 69 | + </syncfusion:TreeViewAdv.ItemTemplate> |
| 70 | + </syncfusion:TreeViewAdv> |
| 71 | +</Grid> |
| 72 | +``` |
| 73 | + |
| 74 | +```csharp |
| 75 | +public partial class MainWindow : Window |
| 76 | +{ |
| 77 | + TreeViewItemAdv drag = null; |
| 78 | + |
| 79 | + public MainWindow() |
| 80 | + { |
| 81 | + InitializeComponent(); |
| 82 | + } |
| 83 | + |
| 84 | + public void DragStart(object sender, DragTreeViewItemAdvEventArgs e) |
| 85 | + { |
| 86 | + drag = (sender as TreeViewAdv).SelectedContainer; |
| 87 | + } |
| 88 | + |
| 89 | + public void DragEnd(object sender, DragTreeViewItemAdvEventArgs e) |
| 90 | + { |
| 91 | + e.Cancel = false; |
| 92 | + TreeViewItemAdv drop = e.TargetDropItem as TreeViewItemAdv; |
| 93 | + |
| 94 | + if (drag != null && drop != null && drag.ParentItemsControl == drop.ParentItemsControl) |
| 95 | + { |
| 96 | + e.Cancel = true; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
0 commit comments