简体   繁体   中英

OnQueryContinueDrag-Event is not triggered when drop occurs

Im currently improving the drag and drop functionality in my wpf application. Therefore I would like to let the source control know when the user drops data on the target control. I think the mechanism for this kind of target is provided by the QueryContinueDrag-event. Actually I was able to successfully set up the integration of the QueryContinueDrag-event:

private void TrvImport_OnPreviewMouseMove(object sender, MouseEventArgs e)
{
    Point mousePos = e.GetPosition(null);
    Vector diff = startPoint - mousePos;

    if (e.LeftButton == MouseButtonState.Pressed
        && (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance
            || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
    {
        MailBox transportBox = new MailBox();
        transportBox.DocumentList = new ObservableCollection<Domain.Document>();

        DataObject dragData = new DataObject("MailBox", transportBox);
        DragDrop.DoDragDrop(TrvImport, dragData, DragDropEffects.All);
    }
}

The event method below is now executed regulary as soon as I start a drag and drop. But when I finish the drag and drop by releasing the mouse button, the event is not fired again with an e.Action == DragAction.Drop argument - as expected by me. Why?

private void TrvImport_OnQueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
    if (e.Action != DragAction.Continue)
    {
        Console.WriteLine("This Line is never executed, why?");
    }
}

I believe the answer is because there is a default event handler for the QueryContinueDrag event which performs all of the necessary functionality required by a drag and drop operation (such as cancelling the operation if the Escape key is pressed). By providing your own event handler, you have overridden the default functionality and not replaced it with your own version.

There really are very few reasons to implement that handler... I have a working drag and drop implementation with custom cursors that does not implement that handler. From the DragDrop.QueryContinueDrag Attached Event page on MSDN:

The QueryContinueDrag event is raised continuously while the drag source is being dragged. You can handle this event to determine what action ends the drag-and-drop operation based on the state of the ESC, SHIFT, CTRL, and ALT keys, as well as the state of the mouse buttons. The default handler for this event cancels the drag-and-drop operation if the ESC key is pressed, and drops the data if the mouse button is released. If you handle this event to change the default behavior, be sure to provide an equivalent mechanism in your handler to end the drag-and-drop operation. Otherwise, the DoDragDrop method will not return and your application will stop responding. If you handle this event, you must mark it as handled to prevent the default behavior from overriding your handler.

Anything that you were trying to do in that handler can be accomplished by implementing a handler for either the PreviewDragOver or PreviewDrop events and using the DragEventArgs object. I hope that helps.


UPDATE >>>

As I said, anything that you were trying to do in the QueryContinueDrag event handler can be accomplished by implementing a handler for either the PreviewDragOver or PreviewDrop events and using the DragEventArgs object. More specifically to answer your comment, you can use the DragEventArgs object to check whether the operation was a Copy or Move operation like this:

if ((e.Effects & DragDropEffects.Copy) == DragDropEffects.Copy)
{
    // It's a copy operation
}
else if ((e.Effects & DragDropEffects.Move) == DragDropEffects.Move)
{
    // It's a move operation
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM