Esempio n. 1
0
        // a version of IsAncestorOf that stops looking when it finds an ancestor
        // of the given type
        internal static bool IsAncestorOf(DependencyObject ancestor, DependencyObject descendant, Type stopType)
        {
            if (ancestor == null)
            {
                throw new ArgumentNullException("ancestor");
            }
            if (descendant == null)
            {
                throw new ArgumentNullException("descendant");
            }

            VisualTreeUtils.EnsureVisual(ancestor);
            VisualTreeUtils.EnsureVisual(descendant);

            // Walk up the parent chain of the descendant until we run out of parents,
            // or we find the ancestor, or we reach a node of the given type.
            DependencyObject current = descendant;

            while ((current != null) && (current != ancestor) && !stopType.IsInstanceOfType(current))
            {
                Visual   visual;
                Visual3D visual3D;

                if ((visual = current as Visual) != null)
                {
                    current = visual.InternalVisualParent;
                }
                else if ((visual3D = current as Visual3D) != null)
                {
                    current = visual3D.InternalVisualParent;
                }
                else
                {
                    current = null;
                }
            }

            return(current == ancestor);
        }