I am using WPF mouse down event on a control. I want to get the X,Y coordinates but I am getting an error:
我在控件上使用WPF鼠标按下事件。我想得到X,Y坐标,但我收到一个错误:
private void button_MouseDown(object sender, MouseButtonEventArgs e)
{
double x = e.X, double y = e.Y;
}
I could not access the coordinates. I wonder why. Can Someone help? If mouse down is unable to get the coordinates, is there other way I can get the coordinate of the cursor when click?
我无法访问坐标。我想知道为什么。有人可以帮忙吗?如果鼠标按下无法获取坐标,有没有其他方法可以在点击时获取光标的坐标?
2 个解决方案
#1
32
You need to use the GetPosition method to retrieve the point.
您需要使用GetPosition方法来检索点。
private void button_MouseDown(object sender, MouseButtonEventArgs e)
{
Point p = e.GetPosition(this);
double x = p.X;
double y = p.Y;
}
#2
-1
Try like
试试吧
C#
private void button_MouseDown(object sender, MouseButtonEventArgs e)
{
double x = e.GetPosition("Name of your element" as IInputElement).X;
}
#1
32
You need to use the GetPosition method to retrieve the point.
您需要使用GetPosition方法来检索点。
private void button_MouseDown(object sender, MouseButtonEventArgs e)
{
Point p = e.GetPosition(this);
double x = p.X;
double y = p.Y;
}
#2
-1
Try like
试试吧
C#
private void button_MouseDown(object sender, MouseButtonEventArgs e)
{
double x = e.GetPosition("Name of your element" as IInputElement).X;
}