简体   繁体   中英

WPF attach popup with a control

I have a scenario where I am displaying a popup on my control. I am able to do this using PlacementTarget , Position , etc.

My problem is when I resize the window and move it to somewhere else in the desktop, the popup remains stationary meaning it doesn't move with the control set as PlacementTarget . I was able to hide it in case of other window getting focus. But not able to attach it with my control.

How can I solve this problem? Is there any other such control available?

If you look at all WPF controls from MS, their Popup in the ControlTemplate is automatically closed when you click somewhere outside the Control. (eg ComboBox)

It's this property responsible for that: StaysOpen="False"

The easiest hack is to define a Window which will look like a Popup and bind its position Properties to your control with a Converter adding some pixels to it.

It is not a perfect solution, but if you have an event to work with, in this case Resize or LocationChanged, you can coax the popup to move by changing one of its offsets and changing it back.

Something like this:

popup.HorizontalOffset += 0.01

popup.HorizontalOffset -= 0.01

I don't think you'll get the behavior you want without deriving from Popup and doing some handling.

If you have the PlacementTarget then you can get the Window that it should follow, right? So when the Popup is showing watch the Window containing the PlacementTarget for movement and adjust the popup accordingly. Popup derives from Window so it should have a Left and Top property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM