Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know if a drag/drop has been cancelled in WPF

I'm writing a user control in WPF which is based on a ListBox. One of the main pieces of functionality is the ability to reorder the list by dragging the items around. When a user drags an item I change the items Opacity to 50% and physically move the item in an ObservableCollection in my ViewModel depending on where the user wants it. On the drop event I change the Opacity back to 100%.

The problem I've got is that if the user drags the item off my control and drops it somewhere else then I need to change the Opacity back to 100% and move the item back to where it was when the user started the drag. Is there an event I can handle to capture this action? If not is there any other cunning way to solve this problem?

like image 461
Jon Mitchell Avatar asked Apr 07 '10 08:04

Jon Mitchell


1 Answers

Assuming you are using the built-in drag and drop functionality, you can use the return value of the DoDragDrop method. If the drop target does not accept the dragged object, then DoDragDrop returns DragDropEffects.None.

This of course assumes that the other controls on your form do not allow the dropping of your list items.

like image 131
itowlson Avatar answered Oct 03 '22 02:10

itowlson