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.
我正在玩一个简单的WPF应用程序。其中一部分包括一个包含多个控件的网格。使用LayoutTransform和RotateTransform旋转网格。我需要获得相对于网格左上角的鼠标点击的坐标,并考虑旋转。
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.
要清楚,假设我在网格中有一个绘图表面,并且没有应用任何变换。然后我点击位置X = 20,Y = 10并在该点处在绘图表面上放置一个点。如果网格现在旋转30度并且我点击点(也通过旋转移动),则点击位置仍应为X = 20,Y = 10。
1 个解决方案
#1
5
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.
MouseEventArgs有一个GetPosition方法,它接受一个UIElement。它返回鼠标事件相对于指定元素的位置。因此,如果要将鼠标单击转换为网格坐标,请将该网格传递给GetPosition方法。
#1
5
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.
MouseEventArgs有一个GetPosition方法,它接受一个UIElement。它返回鼠标事件相对于指定元素的位置。因此,如果要将鼠标单击转换为网格坐标,请将该网格传递给GetPosition方法。