示例#1
0
        /// <summary>
        /// This will search for a parent of the specified type.
        /// </summary>
        /// <typeparam name="T">The type of the element to find</typeparam>
        /// <param name="startingObject">The node where the search begins.</param>
        /// <param name="checkStartingObject">Should the specified startingObject be checked first.</param>
        /// <param name="additionalCheck">Provide a callback to check additional properties
        /// of the found elements. Can be left Null if no additional criteria are needed.</param>
        /// <returns>Returns the found element. Null if nothing is found.</returns>
        /// <example>Button button = TreeHelper.FindParent&lt;Button&gt;( this, foundChild => foundChild.Focusable );</example>
        public static T FindParent <T>(DependencyObject startingObject, bool checkStartingObject, Func <T, bool> additionalCheck) where T : DependencyObject
        {
            T foundElement;
            DependencyObject parent = (checkStartingObject ? startingObject : TreeHelper.GetParent(startingObject, true));

            while (parent != null)
            {
                foundElement = parent as T;

                if (foundElement != null)
                {
                    if (additionalCheck == null)
                    {
                        return(foundElement);
                    }
                    else
                    {
                        if (additionalCheck(foundElement))
                        {
                            return(foundElement);
                        }
                    }
                }

                parent = TreeHelper.GetParent(parent, true);
            }

            return(null);
        }
示例#2
0
        /// <summary>
        /// Returns true if the specified element is a child of parent somewhere in the visual
        /// tree. This method will work for Visual, FrameworkElement and FrameworkContentElement.
        /// </summary>
        /// <param name="element">The element that is potentially a child of the specified parent.</param>
        /// <param name="parent">The element that is potentially a parent of the specified element.</param>
        public static bool IsDescendantOf(DependencyObject element, DependencyObject parent, bool recurseIntoPopup)
        {
            while (element != null)
            {
                if (element == parent)
                {
                    return(true);
                }

                element = TreeHelper.GetParent(element, recurseIntoPopup);
            }

            return(false);
        }
示例#3
0
 /// <summary>
 /// Tries its best to return the specified element's parent. It will
 /// try to find, in this order, the VisualParent, LogicalParent, LogicalTemplatedParent.
 /// It only works for Visual, FrameworkElement or FrameworkContentElement.
 /// </summary>
 /// <param name="element">The element to which to return the parent. It will only
 /// work if element is a Visual, a FrameworkElement or a FrameworkContentElement.</param>
 /// <remarks>If the logical parent is not found (Parent), we check the TemplatedParent
 /// (see FrameworkElement.Parent documentation). But, we never actually witnessed
 /// this situation.</remarks>
 public static DependencyObject GetParent(DependencyObject element)
 {
     return(TreeHelper.GetParent(element, true));
 }