src/Windows/PresentationFoundation/0/src/Utils/VisualTreeUtils.cs
/// <summary>
/// This method is an alternative to WPF's
/// <see cref="VisualTreeHelper.GetParent"/> method, which also
/// supports content elements. Do note, that for content element,
/// this method falls back to the logical tree of the element.
/// </summary>
/// <param name="dependencyObject">The item to be processed.</param>
/// <returns>The submitted item's parent, if available. Otherwise null.</returns>
This method is an alternative to WPF's
method, which also
supports content elements. Do note, that for content element,
this method falls back to the logical tree of the element.
This method is an alternative to WPF ' s method which also supports content elements . Do note that for content element this method falls back to the logical tree of the element .
public static DependencyObject? GetParentObject(DependencyObject? dependencyObject)
        {
            if (dependencyObject is null) {
                return null;
            }
            if (dependencyObject is ContentElement contentElement) {
                var parent = ContentOperations.GetParent(contentElement);
                if (!(parent is null)) {
                    return parent;
                }
                var frameworkContentElement = contentElement as FrameworkContentElement;
                return frameworkContentElement?.Parent;
            }
            return VisualTreeHelper.GetParent(dependencyObject);
        }
public static DependencyObject ? GetParentObject ( DependencyObject ? dependencyObject ) { if ( dependencyObject is null ) { return null ; } if ( dependencyObject is ContentElement contentElement ) { var parent = ContentOperations . GetParent ( contentElement ) ; if ( ! ( parent is null ) ) { return parent ; } var frameworkContentElement = contentElement as FrameworkContentElement ; return frameworkContentElement ? . Parent ; } return VisualTreeHelper . GetParent ( dependencyObject ) ; }
This method is an alternative to WPF's
method, which also
supports content elements.
This method is an alternative to WPF ' s method which also supports content elements .
// If it's not a ContentElement, rely on VisualTreeHelper
The submitted item's parent, if available. Otherwise null.
The submitted item ' s parent if available . Otherwise null .
dependencyObject
DependencyObject?
The item to be processed.
The item to be processed .