Getting the Popup to Close When It Loses Focus
On a recent project, I spent (I kid you not) over three hours trying to get a stinking popup to close when I clicked somewhere else in the application.
Figuring it was a focus issue, I tried the following triggers:
- IsFocused
- IsKeyboardFocused
- IsKeyboardFocusWithin
- IsMouseCapture
- IsMouseCaptureWithin
All to no avail.
Turns out that the only thing I had to do was set the StaysOpen property of the Popup thus:
<Popup StaysOpen=”False“>
Handy little property. I just wish that it was the default.
K Hulme:
thank you. you’ve just ended my 3 hours of mucking about with the same events!
28 December 2007, 2:58 pmknvdss:
Thank you very much. I just spent more than 3 hours on that, trying to handle all kinds of events in the world. Just found it. Thanks for sharing.
23 September 2008, 1:10 pmThameem:
Thanks for sharing you saved my time..
..,
28 October 2008, 11:24 pmmartin:
works fine if your popup has no buttons, textboxes etc (popup closes as soon as you focus the button).
20 November 2008, 1:00 ampurav:
Saved my time. Thanks!
14 May 2009, 2:45 pmsker:
Well, I’ve been working with StaysOpen=”False” all along, and suddenly I started to experience this same behavior. I’m at over 3 hours with this problem and I can’t figure it out. I’ll just keep searching and searching until I grow old and die.
26 November 2009, 3:54 pmGobi:
hi Matthias Shapiro,
Nice
thank u very much for sharing such a wonderful tips..
2 February 2010, 3:18 amVu Minh Khue:
Another way to do it is that you set the popup focus to true first:
22 February 2010, 6:19 am1) popup.Focus();
2) popup.LostFocus += popup_OnLostFocus