开发者

How can I get the type System.Windows.Controls.Primitive.PopupRoot?

开发者 https://www.devze.com 2023-03-30 19:12 出处:网络
Pictures can tell a thousand words. When I climb up the visual tree I see the last parent is of type System.Windows.Controls.Pimitives.PopupRoot

Pictures can tell a thousand words.

When I climb up the visual tree I see the last parent is of type System.Windows.Controls.Pimitives.PopupRoot

How can I get the type System.Windows.Controls.Primitive.PopupRoot?

But Whey I try to actually make a comparison to that type VS开发者_StackOverflow complains it's not valid.

How can I get the type System.Windows.Controls.Primitive.PopupRoot?


PopupRoot is internal to PresentationFramework, so you cannot access it from your assembly. You can compare the type name with GetType().FullName, but PopupRoot is an implementation detail that can change in future framework versions so I wouldn't rely on it.


PopupRoot is internal, so you will not be able to reference it. However, if you use LogicalTreeHelper, you'll be able to find Popup if exists. LogicalTreeHelper will return NULL if there is no logical parent, so you need to use it in addition to walking visual tree with VisualTreeHelper.

Here is an example how you can use it:

var popupRootFinder = VisualTreeHelper.GetParent((DependencyObject)your_visual_element);
while (popupRootFinder != null)
{
    var logicalRoot = LogicalTreeHelper.GetParent(popupRootFinder);
    if (logicalRoot is Popup)
    {
        // popup root found here
        break;
    }

    popupRootFinder = VisualTreeHelper.GetParent(popupRootFinder);
}


If you want to get a Popup object from PopupRoot then you can do it with this code where "d" is type PopupRoot:

Popup customPopup = LogicalTreeHelper.GetParent(d) as Popup;
0

精彩评论

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