WPF Mouse down event no Coordinates

岁酱吖の 提交于 2019-12-23 06:53:28

问题


I am using WPF mouse down event on a control. I want to get the X,Y coordinates but I am getting an error:

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?


回答1:


You need to use the GetPosition method to retrieve the point.

private void button_MouseDown(object sender, MouseButtonEventArgs e)
{
    Point p = e.GetPosition(this);
    double x = p.X;
    double y = p.Y;
}



回答2:


Try like

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


来源:https://stackoverflow.com/questions/17420835/wpf-mouse-down-event-no-coordinates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!