其他分享
首页 > 其他分享> > 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

Will Wang0715 发布了13 篇原创文章 · 获赞 0 · 访问量 122 私信 关注

标签:childOfChild,obj,查看,元素,child,return,WPF,null,ChildItem
来源: https://blog.csdn.net/qq_21209307/article/details/104445208