コード例 #1
0
        /// <summary>
        /// Finds the child visuals of the given type based on the supplied filter functon.
        /// </summary>
        /// <typeparam name="T">The data type of the child visuals to locate.</typeparam>
        /// <param name="visual">The visual whose children of a specific type is desired.</param>
        /// <param name="filter">A filter to be applied to determine if a child of the requested type shall be accepted. If <c>null</c>, all children of the requested type match.</param>
        /// <returns>The child visuals that satisfy the given <paramref name="filter"/>.</returns>
        public static IEnumerable <T> FindChildren <T>(this NativeVisual visual, Predicate <T> filter) where T : NativeVisual
        {
            var children    = new List <T>();
            var numChildren = visual.GetChildVisualCount();

            for (int i = 0; i < numChildren; ++i)
            {
                var child      = visual.GetChildAtIndex <NativeVisual>(i);
                var typedChild = child as T;
                if ((typedChild != null) && ((filter == null) || filter(typedChild)))
                {
                    children.Add(typedChild);
                }
                if (child != null)
                {
                    children.AddRange(child.FindChildren <T>(filter));
                }
            }
            return(children);
        }
コード例 #2
0
        /// <summary>
        /// Finds a child visual of the given type, applying a filter.
        /// </summary>
        /// <typeparam name="T">The data type of the child visual to locate.</typeparam>
        /// <param name="visual">The visual whose child of a specific type is desired.</param>
        /// <param name="filter">A filter to be applied to determine if a child of the requested type shall be accepted. If <c>null</c>, the first child of the requested type matches.</param>
        /// <returns>The first child in the visual tree of the requested type that also satisfies the condition of the filter.</returns>
        public static T FindChild <T>(this NativeVisual visual, Predicate <T> filter) where T : NativeVisual
        {
            var child       = default(T);
            var numChildren = visual.GetChildVisualCount();

            for (int i = 0; (i < numChildren) && (child == null); ++i)
            {
                var typedChild = visual.GetChildAtIndex <T>(i);
                if ((typedChild != null) && ((filter == null) || filter(typedChild)))
                {
                    child = typedChild;
                }
            }
            if (child == null)
            {
                for (int i = 0; (i < numChildren) && (child == null); ++i)
                {
                    child = FindChild <T>(visual.GetChildAtIndex <NativeVisual>(i), filter);
                }
            }
            return(child);
        }