From ce526c9afc72b874080a9b3936a79e90af408b84 Mon Sep 17 00:00:00 2001 From: SweathaBharathi <104504991+SweathaBharathi@users.noreply.github.com> Date: Wed, 13 Aug 2025 19:06:18 +0530 Subject: [PATCH 1/2] Update README.md --- README.md | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8bea624..358e287 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,100 @@ -# How-to-restrict-drag-and-drop-of-nodes-in-WPF-TreeViewAdv -This session explains how to restrict drag and drop of nodes in WPF TreeViewAdv. +# How to restrict drag and drop of parent node into another parent node in WPF TreeViewAdv? -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) +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. + +```csharp +public class Model +{ + public Model() + { + SubItems = new ObservableCollection(); + } + + public string Header { get; set; } + public ObservableCollection SubItems { get; set; } +} +``` + +```csharp +public class ViewModel +{ + public ViewModel() + { + TreeItems = new ObservableCollection(); + PopulateData(); + } + + public ObservableCollection TreeItems { get; set; } + + private void PopulateData() + { + Model root1 = new Model() { Header = "Root1" }; + PopulateSubItems(root1); + TreeItems.Add(root1); + + Model root2 = new Model() { Header = "Root2" }; + PopulateSubItems(root2); + TreeItems.Add(root2); + + Model root3 = new Model() { Header = "Root3" }; + PopulateSubItems(root3); + TreeItems.Add(root3); + } + + private void PopulateSubItems(Model root) + { + Model subItem1 = new Model() { Header = "Item1" }; + Model subItem2 = new Model() { Header = "Item2" }; + Model subItem3 = new Model() { Header = "Item3" }; + Model subItem4 = new Model() { Header = "Item4" }; + + root.SubItems.Add(subItem1); + root.SubItems.Add(subItem2); + root.SubItems.Add(subItem3); + root.SubItems.Add(subItem4); + } +} +``` + +```xml + + + + + + + + + +``` + +```xml +public partial class MainWindow : Window +{ + TreeViewItemAdv drag = null; + + public MainWindow() + { + InitializeComponent(); + } + + public void DragStart(object sender, DragTreeViewItemAdvEventArgs e) + { + drag = (sender as TreeViewAdv).SelectedContainer; + } + + public void DragEnd(object sender, DragTreeViewItemAdvEventArgs e) + { + e.Cancel = false; + TreeViewItemAdv drop = e.TargetDropItem as TreeViewItemAdv; + + if (drag != null && drop != null && drag.ParentItemsControl == drop.ParentItemsControl) + { + e.Cancel = true; + } + } +} +``` From 4df1c26dfc17c77b4d19637539d8861771178747 Mon Sep 17 00:00:00 2001 From: SweathaBharathi <104504991+SweathaBharathi@users.noreply.github.com> Date: Wed, 13 Aug 2025 19:13:46 +0530 Subject: [PATCH 2/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 358e287..1c51678 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ public class ViewModel ``` -```xml +```csharp public partial class MainWindow : Window { TreeViewItemAdv drag = null;