-3

I am new to WPF. In my WPF application button design have status bar. With in status bar I put one TextBlock. That Text Box wants to show current position of the mouse. If I click one control means that wants to show that controls current position. That's want to continuously change when I move cursor

Point position = Mouse.GetPosition(MainWindow);
Cursorposition.Text = "X:" + position.X + "Y:" + position.Y;

its gets only (0,0) I want change dynamically when I move cursor.

your answer ok for mainwindow but in mainwindow I used user control means how i get?

malathi
  • 5
  • 7

3 Answers3

1

Mouse.GetPosition(displayArea); Check this

Davy
  • 134
  • 5
  • I tried but when I move cursor that x and y did not change – malathi Mar 16 '16 at 07:30
  • Share your code, what you have tried so far. – Davy Mar 16 '16 at 07:32
  • Mouse.GetPosition(MainWindow); Make sure MainWindow is the right instance that you are showing. If you are calling this method in MainWindow code behind just pass 'this' like 'Mouse.GetPosition(this);' – Davy Mar 16 '16 at 09:17
0

You should update textblock text "on mouse move" event.

Please check this: https://stackoverflow.com/a/21551437/2792192

Community
  • 1
  • 1
0

Add the following in your MainWindow.xaml in the Window tag

MouseMove="Window_MouseMove"

And your method should look like this:

private void Window_MouseMove(object sender, MouseEventArgs e)
{
    Point position = Mouse.GetPosition(this);
    Cursorposition.Text = "X:" + position.X + "Y:" + position.Y;
}
daniel59
  • 906
  • 2
  • 14
  • 31