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?
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.
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.
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.
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.
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)
.
Use the DragDrop.QueryContinue
event, this allows you to cancel it via the Action
property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With