开发者

WPF attach popup with a control

开发者 https://www.devze.com 2022-12-11 11:25 出处:网络
I have 开发者_开发知识库a scenario where I am displaying a popup on my control. I am able to do this using PlacementTarget, Position, etc.

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. (e.g. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消