4
votes

I have a custom WPF control which handles drag/drop. I override OnDragOver so that the control will not accept the dropped object if it is busy doing something else:

protected override void OnDragOver(DragEventArgs e)
{
     base.OnDragOver(e);

     if (isBusy)     
          e.Effects = DragDropEffects.None;
     else
          e.Effects = DragDropEffects.Move;

     e.Handled = true;
}

In another control which initiates the drag&drop, there is some UI element which is disabled when the operation starts and supposed to be enabled if the operation is cancelled or when the mouse is released on the target while the above target says the operation is not allowed.

What events can I use on the source control to check for the 2nd condition?

2
Simply use the return value of DoDragDrop(). You'll get DragDropEffects.None if the drop was not successful for any reason.Hans Passant

2 Answers

3
votes

As Hans Passant answered in a comment, to check whether the the operation was cancelled you can use the return value, DragDropEffects, of DragDrop.DoDragDrop().

None: The drop target does not accept the data.

Copy: The data is copied to the drop target.

Move: The data from the drag source is moved to the drop target.

Link: The data from the drag source is linked to the drop target.

Scroll: Scrolling is about to start or is currently occurring in the drop target.

All: The data is copied, removed from the drag source, and scrolled in the drop target.

None is the value you are interested in. When the mouse is released, the operation will be cancelled, and DoDragDrop() will return None.

0
votes

While WPF Drag & Drop is in progress, the GiveFeedback event is continuously being fired on the drag source, you can check the event arguments state & update the drag source accordingly.

Here is a code sample: (assuming the element being dragged is called dragSource)

// Attach the event handler
dragSource += OnDragSourceGiveFeedback;

// Event Handler
private void OnDragSourceGiveFeedback(object sender, GiveFeedbackEventArgs e)
    {
        if (e.Effects == DragDropEffects.None)
        {
            // Drop is not allowed on the Drop Target
            dragSource.IsEnabled = false;
        }
    }