Search code examples
c#wpfwindow

WPF Detect Mouse Click out of the Bound of Window


How to detect the action when user click mouse out of Window's bound? And I want to hide it when mouse click out of the Window.

I try the LostFocus Event but it doesn't seem to work:

Window w = new Window();
w.Show();
w.LostFocus += (sender, args) => { w.Hide(); };

Edit:

For the first possible duplicate, it's asking I want the user to be able to move the Window to any position.. and the answer is this.DragMove(); So I think it's not duplicate.

For the second, I think my case is somewaht different from it


Solution

  • You can use Window.Deactivated Event to detect the mouse click out of bound of the Window:

    Window w = new Window();
    w.Show();
    w.Deactivated += (sender, args) => { w.Hide(); };