Skip to content

Commit eb2cf0a

Browse files
ES-975464 - Resolve the ReadMe file length issue in this sample repository
1 parent 50e66fd commit eb2cf0a

File tree

1 file changed

+104
-3
lines changed

1 file changed

+104
-3
lines changed

README.md

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,105 @@
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 nodes in WPF TreeView
32

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 session explains how to restrict drag and drop of nodes in [WPF TreeView](https://help.syncfusion.com/wpf/classic/treeview/overview) (TreeGridAdv).
4+
5+
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.
6+
7+
#### XAML
8+
9+
``` xml
10+
<Grid>
11+
<syncfusion:TreeViewAdv Margin="10"
12+
x:Name="tree"
13+
AllowDragDrop="True"
14+
ItemsSource="{Binding TreeItems}">
15+
<syncfusion:TreeViewAdv.ItemTemplate>
16+
<HierarchicalDataTemplate ItemsSource="{Binding SubItems}">
17+
<TextBlock Text="{Binding Header}" />
18+
</HierarchicalDataTemplate>
19+
</syncfusion:TreeViewAdv.ItemTemplate>
20+
</syncfusion:TreeViewAdv>
21+
</Grid>
22+
```
23+
24+
#### C#
25+
26+
``` csharp
27+
public class Model
28+
{
29+
public Model()
30+
{
31+
SubItems = new ObservableCollection<Model>();
32+
}
33+
34+
public string Header { get; set; }
35+
public ObservableCollection<Model> SubItems { get; set; }
36+
}
37+
```
38+
39+
``` csharp
40+
public class ViewModel
41+
{
42+
public ViewModel()
43+
{
44+
TreeItems = new ObservableCollection<Model>();
45+
PopulateData();
46+
}
47+
48+
public ObservableCollection<Model> TreeItems { get; set; }
49+
50+
private void PopulateData()
51+
{
52+
Model root1 = new Model() { Header = "Root1" };
53+
PopulateSubItems(root1);
54+
TreeItems.Add(root1);
55+
56+
Model root2 = new Model() { Header = "Root2" };
57+
PopulateSubItems(root2);
58+
TreeItems.Add(root2);
59+
60+
Model root3 = new Model() { Header = "Root3" };
61+
PopulateSubItems(root3);
62+
TreeItems.Add(root3);
63+
}
64+
65+
private void PopulateSubItems(Model root)
66+
{
67+
Model subItem1 = new Model() { Header = "Item1" };
68+
Model subItem2 = new Model() { Header = "Item2" };
69+
Model subItem3 = new Model() { Header = "Item3" };
70+
Model subItem4 = new Model() { Header = "Item4" };
71+
72+
root.SubItems.Add(subItem1);
73+
root.SubItems.Add(subItem2);
74+
root.SubItems.Add(subItem3);
75+
root.SubItems.Add(subItem4);
76+
}
77+
}
78+
```
79+
80+
``` csharp
81+
public partial class MainWindow : Window
82+
{
83+
TreeViewItemAdv drag = null;
84+
85+
public MainWindow()
86+
{
87+
InitializeComponent();
88+
}
89+
90+
public void DragStart(object sender, DragTreeViewItemAdvEventArgs e)
91+
{
92+
drag = (sender as TreeViewAdv).SelectedContainer;
93+
}
94+
95+
public void DragEnd(object sender, DragTreeViewItemAdvEventArgs e)
96+
{
97+
e.Cancel = false;
98+
TreeViewItemAdv drop = e.TargetDropItem as TreeViewItemAdv;
99+
if (drag != null && drop != null && drag.ParentItemsControl == drop.ParentItemsControl)
100+
{
101+
e.Cancel = true;
102+
}
103+
}
104+
}
105+
```

0 commit comments

Comments
 (0)