src/Windows/PresentationFoundation/0/src/Utils/VisualTreeUtils.cs
/// <summary>
/// Finds a parent of a given item on the visual tree.
/// </summary>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="dependencyObject">
/// A direct or indirect child of the queried item.
/// </param>
/// <returns>
/// The first parent item that matches the submitted type parameter. 
/// If not matching item can be found, a null reference is being returned.
/// </returns>
Finds a parent of a given item on the visual tree.
Finds a parent of a given item on the visual tree .
[return: MaybeNull]
        public static T GetParentObjectRecursive<T>(DependencyObject dependencyObject)
            where T : DependencyObject
        {
            var parentObject = GetParentObject(dependencyObject);
            if (parentObject == null) {
                return null;
            }
            if (parentObject is T parent) {
                return parent;
            }
            return GetParentObjectRecursive<T>(parentObject);
        }
[ return : MaybeNull ] public static T GetParentObjectRecursive < T > ( DependencyObject dependencyObject ) where T : DependencyObject { var parentObject = GetParentObject ( dependencyObject ) ; if ( parentObject == null ) { return null ; } if ( parentObject is T parent ) { return parent ; } return GetParentObjectRecursive < T > ( parentObject ) ; }
Finds a parent of a given item on the visual tree.
Finds a parent of a given item on the visual tree .
// Get parent item // We've reached the end of the tree // Check if the parent matches the type we're looking for // Use recursion to proceed with next level
The first parent item that matches the submitted type parameter.
If not matching item can be found, a null reference is being returned.
The first parent item that matches the submitted type parameter . If not matching item can be found a null reference is being returned .
dependencyObject
DependencyObject
A direct or indirect child of the queried item.
A direct or indirect child of the queried item .
typeparam
The type of the queried item.
The type of the queried item .