2

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

yu yang Jian
  • 6,680
  • 7
  • 55
  • 80
  • Wait so you posted your question, then answered it at the same time and i mean exact minute? Why post the question then? – P. Pat Mar 06 '17 at 07:51
  • As I know, this is a built in function in SO, named `Answer your own question` http://i.imgur.com/aqR3y0V.png – yu yang Jian Mar 06 '17 at 07:53
  • I am aware of that built in function in SO but all at the same minute? Not saying its wrong but it was even answered the moment question popped out... Like before you asked you already had the answer which reverts back to my question of why post the question then? – P. Pat Mar 06 '17 at 07:56
  • Possible duplicate of [How can I capture mouse events that occur outside of a (WPF) window?](http://stackoverflow.com/questions/13732070/how-can-i-capture-mouse-events-that-occur-outside-of-a-wpf-window) – bradgonesurfing Mar 06 '17 at 07:57
  • Also duplicate of http://stackoverflow.com/questions/10828921/how-to-close-a-wpf-dialog-window-when-the-user-clicks-outside-it – bradgonesurfing Mar 06 '17 at 07:58
  • 1
    answer own quesiton can see: https://stackoverflow.blog/2011/07/01/its-ok-to-ask-and-answer-your-own-questions/ – yu yang Jian Mar 06 '17 at 08:00
  • 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 – yu yang Jian Mar 06 '17 at 08:01
  • All of that is true and you are correct especially when you want to document it in SO for others to benefit but the way you posted it feels slightly off? It also had 1 up vote the moment it was posted. Thank SO for 48 hour rule before being able to accept your own answers. – P. Pat Mar 06 '17 at 08:09
  • I just can say I can't and never upvote or downvote my own quetion since I join here.. and if you check ask own question you will post your Q & A at the same moment – yu yang Jian Mar 06 '17 at 08:14

1 Answers1

5

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(); };
yu yang Jian
  • 6,680
  • 7
  • 55
  • 80