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

FindVisualAncestor() public static method

Finds the visual ancestor according to the predicate.
public static FindVisualAncestor ( 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 FindVisualAncestor(this UIView startElement, Predicate<object> condition, int maxDepth = -1)
        {
            var obj = startElement;
            while ((obj != null) && !condition(obj))
            {
                if (maxDepth == 0)
                {
                    return null;
                }
                else if (maxDepth > 0)
                {
                    maxDepth--;
                }

                obj = obj.GetVisualParent();
            }

            return obj;
        }