DragLeave event delivering wrong value for 'e.GetPosition(...)'
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
_This issue has been moved from [a ticket on Developer Community](https://developercommunity.visualstudio.com/content/problem/635146/dragleave-event-delivering-wrong-value-for-egetpos.html)._
---
While doing Drag'n'Drop I tried to use the DragLeave event to determine, if mouse has left the my control. Problem is, that the DragLeave event is also fired, if mouse leaves toward a child visual of my control but does not leave my control's boundaries at all.
Trying to use the 'getPosition(sender...)' method from DragEventArgs does not return a Point value related to my control, but rather a Point value related to the control it entered while leaving my control.
I would expect a value, that signals that it is out of bounds of the left control, e.g. negative values for leaving toward left and top and positive values greater than width/height for leaving the control toward bottom and right.
---
### Original Comments
#### Visual Studio Feedback System on 7/8/2019, 02:43 AM:
We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.
#### Marie Shi [MSFT] on 7/8/2019, 07:31 PM:
Dear Customer,
Thank you for your feedback! I am unable to reproduce your problem.
For us to investigate this further, could you please provide your detail reproduce steps, sample app to reproduce this issue? We look forward to hearing from you!
Thanks,
Marie
#### Sven Kämpf on 7/22/2019, 07:31 AM:
Hello Marie,
thank you very much for your answer and please excuse my late response (vacation offline :o) ).
Please find attached a sample app to show you the issue (dragleaveissuereproductionapp.tar.gz).
In that sample app I created a rectangle within a canvas. You can drag the rectangle towards the canvas (using left mouse button). If you leave the rectangle's boundaries while dragging, the DragLeave event is fired and handled in the related handler in code-behind:
private void Rectangle_OnPreviewDragLeave(object sender, DragEventArgs e)
{
...}
Here I try to retrieve the relative position of the mouse pointer with:
var givenPoint = e.GetPosition(sender as IInputElement);
Furthermore I relocate the position of the text box (with name "GetPositionValue") to display the coords from the 'givenPoint' and set it's Text property to the 'givenPoint' value:
Canvas.SetLeft(this.GetPositionValue, givenPoint.X);
Canvas.SetTop(this.GetPositionValue, givenPoint.Y);
this.GetPositionValue.Text = givenPoint.ToString();
As you can see (after dragging the rectangle towards the canvas), the textbox is located right at that point, where the mouse left the rectangle on the canvas.
That is, the point given by e.GetPosition(...) is relative to the Canvas(!), even if I gave the sender(i.e. the rectangle) as the InputElement. But the givenPoint's X-value for instance should be negative for leaving the rectangle to the left, etc. Otherwise in my opinion I can not decide, if the drag has left the rectangle's boundaries.
I hope that helped.
PS: I made the app very simple and short. Please ignore code style here.
#### Blair Wang [MSFT] on 7/22/2019, 07:38 AM:
We will close this report in 7 days because we don’t have enough information to investigate further. To keep the problem open, please provide the requested details.
#### Visual Studio Feedback System on 7/23/2019, 03:01 AM:
We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.
#### Sven Kämpf on 8/8/2019, 07:20 AM:
Hello there,
are there any news about this issue? Maybe workarounds or other ideas to avoid that DragLeave?
#### Sven Kämpf on 8/18/2019, 11:38 PM:
Hello again,
could you please tell me the status of my request or a timescale for the answer?
#### Visual Studio Feedback System on 10/8/2019, 02:21 AM:
I have detected that for the last 35 days, this issue didn't have any product team activity and a very small amount of new votes or comments. Based on this, its severity, and affected area, it’s my experience that this issue is very unlikely to be fixed.
#### Sven Kämpf on 10/8/2019, 04:21 AM:
Why? It's essential for Drag&Drop? At least an explanation beside that probably automated Visual Studio Feedback System message would be nice!
---
### Original Solutions
#### Martin Zindler solved on 12/30/2019, 05:00 AM, 0 votes:
Hi,
Described in
https://docs.microsoft.com/en-us/dotnet/api/system.windows.input.mouse.getposition
you may read the section Remarks.
>>>
...
During drag-and drop operations, the position of the mouse cannot be reliably determined through GetPosition.
This is because control of the mouse (possibly including capture) is held by the originating element of the drag
until the drop is completed, with much of the behavior controlled by underlying Win32 calls.
Try the following approaches instead:
- Call the GetPosition method of the DragEventArgs that is passed to the drag events (DragEnter, DragOver, DragLeave).
- Call GetCursorPos, using P/Invoke.
<<<
Another approach will be to use the PreviewMouseMove event.
kind regards // Martin
#### Sven Kämpf solved on 1/6/2020, 00:46 AM, 0 votes:
Hello Martin Zindler,
thank you very much for posting a solution to my issue.
I agree with you, that - using Mouse.GetPosition(...) isn't a good solution. That's why I didn't do it (see my post from july, 22). In my example I wrote "e.GetPosition(...)" where e is a variable containing the DragEventArgs.
I still treat this issue as a bug in the Framework.
For everybody looking for a workaround to use DragLeave:
Since the DragLeave event has another drawback(see below) I would recommend anyone asking me to use DragOver of a Parent in the VisualTree, since this is more reliable.
The other drawback of DragLeave is, that, if you move the mouse too fast over the border of the control you started to drag, the DragLeave event isn't fired sometimes (race condition?). Since this event is only fired once, that missing event distracted my whole application resulting in undefined state.
Using the DragOver event instead is more reliable, since this event is fired multiple times while moving the mouse and you can use a hit test to see, if you left your source control or not.
Contributor guide
Assessment
This issue has not been assessed yet.