In my wpf application, I have some popups (menuContext and dialog) that haven't a parent. But when I open Snoop They have a visual Parent

To get the parent I am using this code:
public static DependencyObject GetParent(this DependencyObject element)
{
DependencyObject dependencyObject;
try
{
dependencyObject = VisualTreeHelper.GetParent(element);
}
catch (InvalidOperationException )
{
dependencyObject = (DependencyObject) null;
}
if (dependencyObject == null)
{
var testec = element.GetName().Contains("Main");
if (element is FrameworkElement frameworkElement2)
dependencyObject = frameworkElement2.Parent;
if (element is FrameworkContentElement frameworkContentElement2)
dependencyObject = frameworkContentElement2.Parent;
}
return dependencyObject;
}
Why for some popups the parent is null? what can I add to this code to get parent =/= null ?
Thanks
I am looking to find the direct parent of popup. I need this to set automationId like this :
MainWindow....Parent.Popup.