Branches need to move the node to a higher level in the parent and lower in the child.

My tree:

<TreeView x:Name="tw_tree" dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" > <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding collection_node, ConverterParameter=name_, Converter={StaticResource MySortConverter}}"> 

On another example, it works fine, but not in mine.

 1 -2 --3 4 -5 --6 

I can move any node to the root or to the same node in which it is located. For example, 3 can be moved to the root or to 2, where it already exists.

To others for some reason does not move.

 public class Node { public string name_ { get; set; } public ObservableCollection<Node> collection_node { get; set; } public Node() { collection_node = new ObservableCollection<Node>(); } } 
  • This is Windows.Forms. TreeView does not contain a definition for ItemDrag - codename0082016
  • Sorry, did not notice the label, just recently dealt with a similar question. Found a working example on WPF code.msdn.microsoft.com/windowsdesktop/… - rdorn

1 answer 1

I recommend using the GongSolutions.WPF.DragDrop , which can be easily installed by adding the appropriate NuGet package.

Further to your TreeView simply add two properties:

 <TreeView dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" ... > 

where dd :

 xmlns:dd="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop" 

Now you can easily drag and drop items:

enter image description here

  • My nodes are dragged only 1 level. - codename0082016
  • @ codename0082016, not sure I understood correctly. Do not you move between levels using this package or in the project should be able to drag only one level? - user227049
  • dragged into the root level only. The levels below (as in the picture) are not dragged. - codename0082016
  • Updated. Anything else to add? - codename0082016
  • @ codename0082016 I can't reproduce the problem, I need a compiled example that I can run and find the cause of the wrong behavior - user227049