Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding Popup control when other window's in focus

I have a custom UserControl which tries to recreate auto-complete for a textbox. When user types, the text is used to filter a provided collection of items and then a Popup displays a ListBox with items that match what user have typed.

Unfortunately, if user decides to switch away from the application to another window (browser, MSWord, anything!), the Popup remains on top of every other window!

Also, if I move my window (which hosts the custom control) with the popup open, the popup stays in place (and doesn't follow the window)! It's kinda funny but obviously not acceptable behaviour. I've looked around but only found one post about this that went unanswered for two years :(

like image 734
Alexandra Avatar asked Apr 23 '09 15:04

Alexandra


2 Answers

Actually, I didn't realize that I had StaysOpen property of the Popup set to true.

<Popup StaysOpen="False" />

actually does the trick for me.

like image 92
Alexandra Avatar answered Sep 21 '22 15:09

Alexandra


I had the same problem in a similar scenario. What I did was I subscribed to all posible "lost focus" events of the control and also got the window which hosts the control and subscribed to its GotMouseCapture and LocationChanged events. Event handlers of all those events are setting the popup's IsOpen property to false.

You can get the hosting window with this:

parentWindow = Window.GetWindow(this);

all other code is simply a lot of subscribing to events to do the same thing.

P.S. I'm not saying it's a pretty or optimal solution, but it works fine for me :)

like image 30
arconaut Avatar answered Sep 21 '22 15:09

arconaut