Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF cancel drag operation

Tags:

wpf

drag

How can i cancel a current drag operation? I want to use the escape key to cancel running drag operations.

Ive looked at the DragDrop class, but cant see anything that would achieve something like DragDrop.Cancel. Any ideas?

like image 821
richzilla Avatar asked Dec 01 '11 10:12

richzilla


People also ask

How to perform a drag-and-drop operation with a drag event handler?

On the drop target, set the AllowDrop property to true. In the drop target, create a Drop event handler to process the dropped data. In the Drop event handler, extract the data from the DragEventArgs by using the GetDataPresent and GetData methods. In the Drop event handler, use the data to perform the desired drag-and-drop operation.

What is drag and drop in WPF?

Drag-and-drop supports manipulating objects within a single application, or between different applications. Dragging-and-dropping between WPF applications and other Windows applications is also fully supported. In WPF, any UIElement or ContentElement can participate in drag-and-drop.

How do you use drag and drop in uielement?

A drag source can be a UIElement or a ContentElement. Create an event handler on the drag source that will initiate the drag-and-drop operation. The event is typically the MouseMove event. In the drag source event handler, call the DoDragDrop method to initiate the drag-and-drop operation.

What is a bubbling event in drag and drop?

This event occurs continuously during a drag-and-drop operation, and enables the drop source to give feedback information to the user. This feedback is commonly given by changing the appearance of the mouse pointer to indicate the effects allowed by the drop target. This is a bubbling event.


2 Answers

I solved the cancelling operation using the following:

On the Control performing the drag (DataGrid in my case) I added a handler for the QueryContinueDrag event.

private void DataGrid_QueryContinueDrag(object sender,
                                        QueryContinueDragEventArgs e)
{
    if (... condition ...)
        e.Action = DragAction.Cancel;
}

The condition in your case would be Keyboard.IsKeyDown(Key.Escape).

like image 78
Honza Pačuta Avatar answered Oct 15 '22 09:10

Honza Pačuta


Use the DragDrop.QueryContinue event, this allows you to cancel it via the Action property.

like image 43
Bas Avatar answered Oct 15 '22 07:10

Bas