Example #1
0
        /// <summary>
        /// Performs the specified operation for the ancestor element (if desired), as well as each
        /// descendant of the given type in the visual tree.
        /// </summary>
        public static VisualTreeForEachResult ForEachChildOfType <T>(this DependencyObject element, VisualTreeFindFlags findFlags, VisualTreeForEachTypedHandler <T> handler) where T : class
        {
            var  result      = VisualTreeForEachResult.Continue;
            bool skipCurrent = (findFlags & VisualTreeFindFlags.ExcludeCurrentElement) == VisualTreeFindFlags.ExcludeCurrentElement;

            findFlags &= ~VisualTreeFindFlags.ExcludeCurrentElement;

            T asT = null;

            if (!skipCurrent)
            {
                // Check if the element is of the supplied type
                asT = element as T;
            }

            if (asT != null)
            {
                result = handler(asT);
            }

            if (result == VisualTreeForEachResult.Continue)
            {
                var asDO = element as DependencyObject;
                if (asDO != null)
                {
                    // Walk visual child list and look for more (recurse)
                    int count = VisualTreeHelper.GetChildrenCount(asDO);
                    for (int i = 0; i < count; i++)
                    {
                        var child = VisualTreeHelper.GetChild(asDO, i);
                        result = ForEachChildOfType <T>(child, findFlags, handler);
                        if (result == VisualTreeForEachResult.Stop)
                        {
                            break;
                        }
                    }
                }
            }

            if (result == VisualTreeForEachResult.SkipChildrenAndContinue)
            {
                result = VisualTreeForEachResult.Continue;
            }

            return(result);
        }
Example #2
0
 /// <summary>
 /// Performs the specified operation for the ancestor element, as well as each
 /// descendant of the given type in the visual tree.
 /// </summary>
 public static VisualTreeForEachResult ForEachChildOfType <T>(this DependencyObject element, VisualTreeForEachTypedHandler <T> handler) where T : class
 {
     return(ForEachChildOfType <T>(element, VisualTreeFindFlags.None, handler));
 }