简体   繁体   中英

How to anchor a WPF Popup to a Control?

I have decided to use templated PopUps to display validation errors for my controls, rather than ToolTips (seem to offer greater flexibility in terms of styling). I am looking for a clean way to ensure that the PopUps are anchored to the control they are referring to so when the window is moved they are moved with them ...

One thought i had was to traverse the visual tree and manually set all PopUps IsOpen property to false...this seems like a bit of a "hack". It is less than ideal as it will presumably flicker among other things...Having said this a lot of the solutions i have seen so far on stackoverflow also seem to have their pitfalls. Ideas?

// Reference to the PlacementTarget.
DependencyObject myPopupPlacementTarget;

// Reference to the popup.
Popup myPopup; 

Window w = Window.GetWindow(myPopupPlacementTarget);
if (null != w)
 {
w.LocationChanged += delegate(object sender, EventArgs args)
{
    var offset = myPopup.HorizontalOffset;
    myPopup.HorizontalOffset = offset + 1;
    myPopup.HorizontalOffset = offset;
    };


}

From this link

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