What you want is unfortunately hidden behind an internal property of the DependencyObject
InheritanceContext
so the only way to access it is via reflection. So if you are comfortable with that then this solution will work.
public class Prototype
{
[Test, RequiresSTA]
public void HackReflectionGetParent()
{
var lw = new ListView();
var view = new GridView();
var gvc = new GridViewColumn();
view.Columns.Add(gvc);
lw.View = view;
var resolvedLw = gvc.GetParents().OfType<ListView>().FirstOrDefault();
Assert.AreEqual(lw, resolvedLw);
}
}
public static class DependencyObjectExtensions
{
private static readonly PropertyInfo InheritanceContextProp = typeof (DependencyObject).GetProperty("InheritanceContext", BindingFlags.NonPublic | BindingFlags.Instance);
public static IEnumerable<DependencyObject> GetParents(this DependencyObject child)
{
while (child != null)
{
var parent = LogicalTreeHelper.GetParent(child);
if (parent == null)
{
if (child is FrameworkElement)
{
parent = VisualTreeHelper.GetParent(child);
}
if (parent == null && child is ContentElement)
{
parent = ContentOperations.GetParent((ContentElement) child);
}
if (parent == null)
{
parent = InheritanceContextProp.GetValue(child, null) as DependencyObject;
}
}
child = parent;
yield return parent;
}
}
}
There was some discussion about this being made public back in 2009 and nothing has happened so I doubt it will be. That said the property is used extensively within the framework and is Friend Visible to other framework assemblies so I think it is a pretty safe bet it isn't going to change soon either.