Пример #1
0
        public static TChild GetLastVisualChild <TChild>(this DependencyObject obj, Predicate <TChild> predicate = null)
            where TChild : DependencyObject
        {
            if (obj == null)
            {
                return(null);
            }
            var count = VisualTreeHelper.GetChildrenCount(obj);

            for (int i = count - 1; i >= 0; i--)
            {
                var c = VisualTreeHelper.GetChild(obj, i);
                var r = c as TChild;
                if (r != null && (predicate == null || predicate(r)))
                {
                    return(r);
                }
                r = GetLastVisualChild(c, predicate);
                if (r != null)
                {
                    return(r);
                }
            }
            return(null);
        }
Пример #2
0
        private static IEnumerable <DependencyObject> GetAllVisualChildren(DependencyObject source)
        {
            var stack = new Queue <DependencyObject>();

            stack.Enqueue(source);

            while (stack.Any())
            {
                DependencyObject current = stack.Dequeue();
                int childCount           = WPFVisualTreeHelper.GetChildrenCount(current);
                for (int i = 0; i < childCount; i++)
                {
                    var child = WPFVisualTreeHelper.GetChild(current, i);
                    yield return(child);

                    stack.Enqueue(child);
                }
            }
        }
Пример #3
0
        public static IEnumerable <T> GetVisualChildren <T>(this DependencyObject source) where T : DependencyObject
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            return(GetVisualChildrenImplementation());

            IEnumerable <T> GetVisualChildrenImplementation()
            {
                int count = WPFVisualTreeHelper.GetChildrenCount(source);

                for (int i = 0; i < count; i++)
                {
                    if (WPFVisualTreeHelper.GetChild(source, i) is T child)
                    {
                        yield return(child);
                    }
                }
            }
        }
Пример #4
0
        public static TChild GetFirstVisualChild <TChild>(this DependencyObject obj)
            where TChild : DependencyObject
        {
            var count = VisualTreeHelper.GetChildrenCount(obj);

            for (int i = 0; i < count; i++)
            {
                var c = VisualTreeHelper.GetChild(obj, i);
                var r = c as TChild;
                if (r != null)
                {
                    return(r);
                }
                else
                {
                    r = GetFirstVisualChild <TChild>(c);
                    if (r != null)
                    {
                        return(r);
                    }
                }
            }
            return(null);
        }
Пример #5
0
 public static DependencyObject GetFirstVisualChild(this DependencyObject obj)
 {
     return(VisualTreeHelper.GetChild(obj, 0));
 }
Пример #6
0
 public static DependencyObject GetParent(this DependencyObject obj)
 {
     return(obj is System.Windows.Media.Visual ? VisualTreeHelper.GetParent(obj) : LogicalTreeHelper.GetParent(obj));
 }
Пример #7
0
 public static DependencyObject GetVisualParent(this DependencyObject obj)
 {
     return(VisualTreeHelper.GetParent(obj));
 }