14

After having started a Drag & Drop operation by DragDrop.DoDragDrop(...) no more MouseMove Events are fired. I even tried

AddHandler(Window.MouseMoveEvent, new MouseEventHandler(myControl_MouseMove), true); 

where the last parameter means I even opt in for handled events. No chance, seems like the MouseMove Event is never fired at all! Any way to still get MouseMove Events while using Drag & Drop? I'd like to Drag & Drop a control, while dragging this control it shall follow the mouse pointer. Any idea how to do this in this case?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
  • This message thread also confirms exactly what you're seeing: http://social.msdn.microsoft.com/Forums/en/wpf/thread/1053aaa4-d8b6-48d7-8d53-2af98e60d542 – Scott Whitlock Aug 16 '10 at 00:45

2 Answers2

9

You need to handle the DragOver event.

EDIT: Try handling the GiveFeedback event on the control that you called DoDragDrop on; that might do what you're looking for.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • thanks for your reply, so I would have to add dragover to all controls in my window and let them point to one single event handler that moves my control? – stefan.at.kotlin Apr 19 '10 at 02:47
  • I finally used the WinApi to get the cursor position, but using dragover is also used sometimes. See http://blogs.msdn.com/b/jaimer/archive/2007/07/12/drag-drop-in-wpf-explained-end-to-end.aspx. – stefan.at.kotlin Jul 05 '10 at 16:32
  • I used Snoop to help me see that my code was firing the DragOver event and not the MouseMove event. See: http://blois.us/Snoop/ – Zamboni Jul 28 '10 at 22:23
  • 5
    The problem is that DragOver only fires when you enter the bounds of a control that has AllowDrop set to True. It doesn't re-fire as the mouse moves around inside it. So you can't adjust the visual feedback based on the mouse position. GiveFeedBack only fires on the source, not the one you're hovering over. – Scott Whitlock Aug 16 '10 at 00:48
  • Try to use PreviewGiveFeedback on App.Current.MainWindow – Necriis Dec 21 '12 at 15:10
  • BTW ... the DragDrop class doesn't exist until .NET 3.0, but Controls also have [DragOver](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dragover%28v=vs.80%29.aspx) and [GiveFeedback](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.givefeedback%28v=vs.80%29.aspx) events. – reinierpost Jul 30 '13 at 12:54
  • @reinierpost: WPF itself didn't exist until .Net 3.0. You're confusing it with WinForms. – SLaks Jul 30 '13 at 21:14
  • @Slaks: sorry I didn't realise this question was specifically about WPF. I'm here because your answer almost answers the same question for my Windows.Forms application. – reinierpost Jul 31 '13 at 12:48
0

What is the DragDrop.DoDragDrop construction? DoDragDrop is intended to be called in MouseDown/MouseMove method indeed, not in DragDrop. It is supposed to START handling the procedure, not to react for the drop (the desired effect of the drop you just implement directly in DragDrop). MouseMove never fires while already dragging, perhaps that's why it does not also fire with you, since you set the procedure. I think you handle this wrong way, here is one of examples http://msdn.microsoft.com/en-us/library/aa984430%28v=vs.71%29.aspx.

bloody
  • 1,131
  • 11
  • 17