其他分享
首页 > 其他分享> > WPF查看子元素

WPF查看子元素

作者:互联网

原文:WPF查看子元素

查找子元素

private static ChildItem FindVisualChildItem<ChildItem>(DependencyObject obj) where ChildItem : DependencyObject
        {
            if (null != obj)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child != null && child is ChildItem)
                        return (ChildItem)child;
                    else
                    {
                        ChildItem childOfChild = FindVisualChildItem<ChildItem>(child);
                        if (childOfChild != null)
                            return childOfChild;
                    }
                }
            }
            return null;
        }

        private static ChildItem FindVisualChildItem<ChildItem>(DependencyObject obj, string name) where ChildItem : FrameworkElement
        {
            if (null != obj)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child != null && child is ChildItem && (child as ChildItem).Name.Equals(name))
                    {
                        return (ChildItem)child;
                    }
                    else
                    {
                        ChildItem childOfChild = FindVisualChildItem<ChildItem>(child, name);
                        if (childOfChild != null && childOfChild is ChildItem && (childOfChild as ChildItem).Name.Equals(name))
                        {
                            return childOfChild;
                        }
                    }
                }
            }
            return null;
        }

摘抄自CSDN专家,muzizongheng

标签:childOfChild,obj,查看,元素,child,return,WPF,null,ChildItem
来源: https://www.cnblogs.com/lonelyxmas/p/12965088.html