views:

1055

answers:

3

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

A: 

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.

RandomEngy
ComboBox uses Popup, and that stays in position as the ComboBox moves.. so it must be possible
Schneider
+1  A: 

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

arconaut
+2  A: 

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.

Alexandra