Detecting drop (Drag and Drop between a WPF and a Non-WPF application)

When you want to enable drag & drop between a WPF and a Non-WPF application where Drag is originated from the WPF application and Drop happens on the Non-WPF application, detecting when the Drop happened could be a challenge.


You can achieve this 2 ways:

Approach 1:
DragDrop.DoDragDrop is a blocking call, thus when this call returns you can be sure that the drop happened. The drop could have happened in the same application or the other application, you would always know on the application that started the drag.

Here is the DragDrop.DoDragDrop method signature:


Code Sample:

....
....
DragDrop.DoDragDrop(_draggedElt, data, DragDropEffects.All);
// Drop happened
{

}



Approach 2:
Before calling DragDrop.DoDragDrop attach the following event handler to the drag source:
 
var queryhandler = new QueryContinueDragEventHandler(DragSourceQueryContinueDrag);
_dragSource.QueryContinueDrag += queryhandler;

While Drag is happening, the following event handler is called continuously. You can monitor the Argument’s States, when the state is DragDropKeyStates.None you can be certain that Drop happened.

private void DragSourceQueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
         if (e.KeyStates == DragDropKeyStates.None)
            {
                //DropHappened();
            }
}

Comments

Popular posts from this blog

WPF How to Dispose ViewModel when the associated UserControl (Not Window) closes?

C# How to unit test Dispatcher

WPF: How to Deep Copy WPF object (e.g. UIElement) ?