Export to GitHub

gong-wpf-dragdrop - issue #35

About DragInfo.DragStartPosition - e.GetPosition(null) is misleading when using with Context menu


Posted on Apr 27, 2011 by Helpful Bird

I would begin with that, this is a wonderful framework for Drag and Drop in wpf. Ok, with that said, there was a small issue that I had been facing. I just wanted to report that and also how I resolved it? I was trying this framework with a ListView. My listview control is my DragSource(=True). Incidently, the listview also has its context menu. Now the problem arises when I right-click on an item and then (just to close the context menu) click anywhere (sometimes on one of the items) on the listview. Now when I would click on one of the items, it sets System.Windows.DragDrop.DoDragDrop(,,,), in the DragSource_PreviewMouseMove event handler, even though I had just clicked the left button (not dragged it). It would have been fine otherwise, but in this case it does not fire the PreviewMouseLeftButtonUp event, with the result that my Mouse pointer stops taking any input. I am not able to click on any other button on the window untill I minimize and then maximize the window. Now here is the list of events that could be reason of the problem; 1.I right click on an item in listview to bring-up the Context menu. 2.I donot click on the Context menu item and instead click on some listviewitem again to close the context menu -here, first PreviewMouseLeftButtonDown is called which is handled by DragSource_PreviewMouseLeftButtonDown -inside this handler this code, "mDragInfo = new DragInfo(sender, e);" creates mDragInfo -inside the ctor of mDragInfo, this code, "DragStartPosition = e.GetPosition(null);" sets the DragStartPosition, BUT RELATIVE TO MY CONTEXT MENU -then, PreviewMouseMove is called which is handled by DragSource_PreviewMouseMove -here, this code, "Point position = e.GetPosition(null);" takes the position again, BUT THIS TIME RELATIVE TO MY LISTVIEW -and when it compares the two positions in the code below, it evaluates to True; if (Math.Abs(position.X - dragStart.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(position.Y - dragStart.Y) > SystemParameters.MinimumVerticalDragDistance) -and this leads to setting the System.Windows.DragDrop.DoDragDrop(,,,) which wouldn't have otherwise happened if it would be a simple mouse-click on a list view

To resolve it, I simply took "sender" as argument for the two e.GetPosition() statements (one in the ctor of DragInfo and the other in DragSource_PreviewMouseMove event handler). Since, in my case the sender in context of both the statements was the same listview, so I could get it right.

Comment #1

Posted on Jun 6, 2011 by Grumpy Rabbit

See my fix for issue 22.

Status: New

Labels:
Type-Defect Priority-Medium