BlackWasp
BlackWasp

Reputation: 4971

How do I get the co-ordinates of a mouse click on a transformed WPF control?

I'm playing with a simple WPF application. One part of it includes a grid containing several controls. The grid is rotated using a LayoutTransform and a RotateTransform. I need to get the co-ordinates of a mouse-click relative to the top-left of the grid, taking the rotation into account.

To be clear, let's say I have a single drawing surface within the grid and no transform had been applied. I then click at location X = 20, Y = 10 and put a dot on the drawing surface at that point. If the grid is now rotated by 30 degrees and I click on the dot (which is also moved by the rotation), the click position should still be X = 20, Y = 10.

Upvotes: 3

Views: 386

Answers (1)

Josh
Josh

Reputation: 69272

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

Upvotes: 5

Related Questions