WPF pins control popup

I have a scenario where I show a popup on my control. I can do this with PlacementTarget , Position , etc.

My problem is that when I resize the window and move it to another location on the desktop, the pop-up window remains stationary, which means that it does not move using the PlacementTarget control set. I was able to hide it if another window got focus. But could not attach it with my control.

How can I solve this problem? Is there still such control?

+4
source share
4 answers

If you look at all the WPF controls from MS, their pop-up window in the ControlTemplate automatically closes when you click somewhere outside the control. (e.g. ComboBox)

This property is responsible for this: StaysOpen="False"

The easiest hack is to define a Window that will look like a Popup and bind its Properties position to your control using a converter that adds a few pixels to it.

+4
source
John Christman (above) has the right idea. I posted an answer to a similar question that shows this approach in context. It handles cases of moving or changing a window.
+2
source

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

Something like that:

popup.HorizontalOffset + = 0.01

popup.HorizontalOffset - = 0.01

+1
source

I do not think that you will get the right behavior without getting Popup and not coping.

If you have a PlacementTarget, you can get a window in which it should follow, right? Therefore, when a popup window displays a Window containing the PlacementTarget to move and adjusts the popup accordingly. Popup comes from a window, so it must have the Left and Top property.

0
source

All Articles