23

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?

Ji yong
  • 433
  • 2
  • 5
  • 12

2 Answers2

48

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;
}
keyboardP
  • 68,824
  • 13
  • 156
  • 205
0

Try like

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