I have a WPF 4 app which I want to enable drag and drop with, currently I have it working with a basic drag and drop implementation, but I have found that it would be much better if, instead of the mouse cursor changing over to represent the move operation, I could use an image underneath my finger.
My drag and drop operation is initiated inside a custom user control, so I will need to insert a visual element into the visual tree and have it follow my finger around, perhaps I should enable the ManipulationDelta
event on my main window, check for a boolean then move the item around?
Best Answer
From the mentioned article I was able to simplify a little. Basically what you need to do is subscribe in 3 events:
DragDrop.DoDragDrop
AllowDrop
set totrue
in order to accept drops)DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move);
the first parameter is the element you are dragging, then the second is the data it is carrying and last the mouse effect.This method locks the thread. So everything after its call will only execute when you stop dragging.
In the drop event you can retrieve the data you sent on the
DoDragDrop
call.The source for my tests are located bellow, and the result is:
Sample drag n' drop (gif)
Full Source
MainWindow.xaml
Card.xaml
MainWindow.xaml.cs
Card.xaml.cs