wpf WPF鼠标按下事件没有坐标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17420835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:07:41  来源:igfitidea点击:

WPF Mouse down event no Coordinates

wpfmousedown

提问by Ji yong

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?

我无法访问坐标。我想知道为什么。有人可以帮忙吗?如果鼠标按下无法获取坐标,是否有其他方法可以在单击时获取光标的坐标?

回答by keyboardP

You need to use the GetPositionmethod 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;
}

回答by Ravuthasamy

Try like

试试像

C#
private void button_MouseDown(object sender, MouseButtonEventArgs e)
    {
        double x = e.GetPosition("Name of your element" as IInputElement).X;
    }