I am trying to implement a drag drop functionality in wpf form. The aim is to implement make a copy of usercontrol by dragging it (similar to windows 7 folder copy using ctrl + Mouseleftbutton drag and drop). The drop works fine when the ctrl key is not down, but when the ctrl key is down the drop event is not getting fired. How can i detect a drop when ctrl key is down?
1 Answers
You don't need to detect key presses to accomplish what you're after. It all depends on what the value of the (DragEventArgs).AllowedEffects property. This is initially set when you call the DragDrop.DoDragDrop method:
DragDrop.DoDragDrop(dragSource, data, DragDropEffects);
Here, the DragDropEffects property represents an enumeration of type DragDropEffects. If you set this to DragDropEffects.Copy | DragDropEffects.Move, then you should be able to move or copy (by holding down the Ctrl key):
DragDrop.DoDragDrop(dragSource, data, DragDropEffects.Copy | DragDropEffects.Move);
Now in one of the drag and drop handlers that receives a parameter of type DragEventArgs, you should see that the (DragEventArgs).AllowedEffects property will have the value that you set in the DragDrop.DoDragDrop method. It just remains for you to set the (DragEventArgs).Effects property to the same value to 'allow' both operations to take place when the user releases the mouse button.