How can I listen to left mouseclicks on canvas in C # WPF?

I try to listen for a mouse click anywhere in my window (except where the buttons are, but I will deal with this later) and then return the point (x, y) of the location.

here is the corresponding code:

 private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Console.WriteLine("mouseLeft is clicked");
    Point x = e.MouseDevice.GetPosition(this);
    Console.WriteLine(x.X);
    Console.WriteLine(x.Y);
}

<Canvas MouseLeftButtonDown="Grid_MouseLeftButtonDown">

When I click, nothing is printed. What am I doing wrong? The first method is inside mainWindow.Xaml.cs.

early.

+3
source share
2 answers

Set the background transparent and set the canvas size!

+5
source

GUI, Debug.WriteLine, MessageBox.Show , , .

, , .

+3

Source: https://habr.com/ru/post/1774370/


All Articles