How to get mouse click coordinates on a converted WPF control?

I play with a simple WPF application. One part of it contains a grid containing several controls. The grid is rotated using LayoutTransform and RotateTransform. I need to get the coordinates of the mouse click relative to the upper left edge of the grid, taking into account the rotation.

To be clear, let's say I have one drawing surface inside the grid, and no transformation has been applied. Then I click at the point X = 20, Y = 10 and place the point on the drawing surface at that point. If the grid is now rotated 30 degrees, and I click on a point (which also moves in rotation), the position of the click should be equal to X = 20, Y = 10.

+3
source share
1 answer

MouseEventArgs has a GetPosition method that accepts a UIElement. It returns the position of the mouse event relative to the specified element. Therefore, if you want to convert a mouse click into the grid coordinates, pass this grid to the GetPosition method.

+5
source

Source: https://habr.com/ru/post/1744641/


All Articles