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
But Whey I try to actually make a comparison to that type VS开发者_StackOverflow complains it's not valid.
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;
精彩评论