I’m having some troubles getting DragDrop.DoDragDrop to work along nicely with a left click event.
My control has several links which can either be dragged around or left clicked to visit.
I currently subscribe to the preview mouse move event which is where i launch the drag and drop event if the left mouse button is pressed.
In another call back i handle the mouse left button down and up event to check for a click. Is there anyway to check if DragDrop actually had a drag drop event take place?
See this link drag drop in wpf explained end to end and scroll down a little to the section “Detecting Drag & Drop”
Code inserted here encase the blog goes missing…
From [http://msdn2.microsoft.com/en-us/library/aa289508(vs.71).aspx] Here is the sequence of events in a typical drag-and-drop operation:
Detecting Drag & Drop
Before the DoDragDrop is called, we must detect a mouse Drag operation on the source… A mouse drag is usually a MouseLeftButtonDown + a MouseMove (before MouseLeftButton goes up).
So, our drag & drop source control needs to subscribe to these two events:
To prevent from starting a false drag & drop operation where the user accidentally drags, you can use
One way to do this is on MouseLeftButtonDown, record the starting position and onMouseMove check if the mouse has moved far enough..
Its a Drag .. now what?
The data! You need to find out what is under the mouse when dragging.
I will omit take the easy way out and assume that whoever is triggering the MouseMove is what I want to drag .. so look at MouseEventArgs.OriginalSource.. [or you could do some 2D HitTesting using VisualTreeHelper .. In Part3 of this write up will try to walk you through hit testing the listbox -which is the other common scenario I encounter-.
Once you have the object to drag, you will need to package what you are a sending into a DataObject that describes the data you are passing around.
DataObject is a wrapper to push generic data (identified with extensible formats) into drag/drop.. As long as both the source and destination understand the format, you will be set. As such, DataObject has a couple interesting methods:
Not much interesting stuff here.. In the sample I just hard-coded my data to be of type string… this makes it easier to paste into external containers (for example Word, which you can use to test this part of the write-up). I do have to stress that drag & dropping should be about the data …
Providing visual feedback during the drag & drop operation..
Before we call DoDragDrop () we have a few ‘choices’ to make around the feedback we want to provide and the ‘scope’ of the d&d.
Simplest scenario: No custom cursor and we want it to drag across apps:
If you don’t want a fancy cursor, you are done!! You can call DoDragDrop directly …
Note: this code allows you to drag & drop across processes, it uses the default operating system feedback ( e.g. + for copy)..