
I discovered that if you do a VisualTreeHelper.HitTest(reference,location) call, a visual gets returned that represents the control you clicked on. Code to move the item in the model is placed here. Task targetTask = GetItemAtLocation(MouseUtilities.GetMousePosition()) Task sourceTask = (Task)e.Data.GetData(typeof(Task)) void treeView_Drop(object sender,DragEventArgs e) Because WPF has a separated visual tree, this can' t be done using normal methods. In this eventhandler I had another problem, how to get the target item to drop the source item on. This is done by moving the dragged item to another position in the tree and setting the isDragging flag to false again, so that the user can issue another drag-drop movement. Last you need to finish the drag-drop movement in the Drop eventhandler. The code here will look something like this: void treeView_DragOver(object sender,DragEventArgs e) Next you need to check if the user is doing the correct thing in the DragOver eventhandler, you can manipulate the behaviour of the drag-drop movement by setting the Effects property of the passed DragEventArgs object. This issues the drag-drop movement and ensures that the user can' t start the same operation a second time.

If(!_isDragging & e.LeftButton = MouseButtonState.Pressed)ĭragDrop.DoDragDrop(treeView,treeView.SelectedValue, In the MouseMove eventhandler you have to add code similar to the following snippet: void treeView_MouseMove(object sender,MouseEventArgs e) It' s as simple as implementing three eventhandlers: MouseMove, DragOver and Drop. Appearantly I wasn' t the only person who noticed this weird behaviour, because Josh Smith tried to do something similar and had to write a wrapper for the Win32 mouse api to get the correct result.Īfter using this utility I had the whole thing working pretty quickly. I tried different variations on this, but to no avail. When you issue a DragDrop.DoDragDrop operation within a mousemove eventhandler, the mousemove event no longer gets fired, until you drop the item. As it turns out there' s a problem with the mouse eventhandling.

However, getting items dragged from one point in the tree to another point in the tree is a whole different story. Creating the basic structure was rather pleasing work to do because setting up databinding and getting the items to render correctly is a breeze with the new layout and databinding capabilities.

Last week I was trying to get a drag and drop treeview to work in WPF.
