Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

reflection

wpf

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 enter image description here

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

enter image description here

like image 281
Joel Barsotti Avatar asked Aug 23 '11 19:08

Joel Barsotti


2 Answers

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.

like image 181
Julien Lebosquain Avatar answered Oct 14 '22 15:10

Julien Lebosquain


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);
}
like image 40
zmechanic Avatar answered Oct 14 '22 16:10

zmechanic