Catel.MVVM.Views.ViewExtensions.FindLogicalOrVisualAncestor C# (CSharp) Method

FindLogicalOrVisualAncestor() public static method

Finds the logical or visual ancestor according to the predicate.
public static FindLogicalOrVisualAncestor ( this startElement, Predicate condition, int maxDepth = -1 ) : object
startElement this The start element.
condition Predicate The condition.
maxDepth int The maximum number of levels to go up when searching for the parent. If smaller than 0, no maximum is used.
return object
        public static object FindLogicalOrVisualAncestor(this UIView startElement, Predicate<object> condition, int maxDepth = -1)
        {
            // Try to find visual ancestor one level up
            object visualAncestor = FindVisualAncestor(startElement, condition, 1);
            if (visualAncestor != null)
            {
                return visualAncestor;
            }

            // If we didn't find anything, try visual parent and call this method (recursive)
            var visualParent = startElement.GetVisualParent();
            if (visualParent != null)
            {
                object lastResortVisualAncestor = FindLogicalOrVisualAncestor(visualParent, condition);
                if (lastResortVisualAncestor != null)
                {
                    return lastResortVisualAncestor;
                }
            }

            return null;
        }