From eb2cf0add0e439713ccf18c86b8f3f579c87c993 Mon Sep 17 00:00:00 2001 From: Sreemon Premkumar M Date: Fri, 22 Aug 2025 13:28:12 +0530 Subject: [PATCH] ES-975464 - Resolve the ReadMe file length issue in this sample repository --- README.md | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8bea624..32bceb4 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,105 @@ -# 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 nodes in WPF TreeView -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 session explains how to restrict drag and drop of nodes in [WPF TreeView](https://help.syncfusion.com/wpf/classic/treeview/overview) (TreeGridAdv). + +To restrict drag and drop of a parent node into another parent node, where it will allow only reordering the child nodes in `TreeViewAdv`. 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. + +#### XAML + +``` xml + + + + + + + + + +``` + +#### C# + +``` 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); + } +} +``` + +``` csharp +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; + } + } +} +``` \ No newline at end of file