Alexandra
Alexandra

Reputation: 4853

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 :(

Upvotes: 8

Views: 7483

Answers (3)

Alexandra
Alexandra

Reputation: 4853

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.

Upvotes: 15

arconaut
arconaut

Reputation: 3285

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 :)

Upvotes: 4

RandomEngy
RandomEngy

Reputation: 15413

According to the Popup documentation:

When Popup is displayed on the screen, it does not reposition itself if its parent is repositioned.

So it does not look like it would be a very good candidate for an autocomplete textbox. I think the class is meant more for showing information when you hover over an item.

Upvotes: 0

Related Questions