zoukankan      html  css  js  c++  java
  • silverlight遍历子元素 Get Descendant ,Childern

    项目中遇到需要获取一个元素的所有子元素或后代元素, 系统有FindName("Name"),但我们需要的元素大部分都没有name,而且FindName只能返回一个结果。我们扩展了  方法,解决的获取所有子元素或后代元素

           public static List<DependencyObject> GetChildern(this DependencyObject root)
            {
                if (root == null)
                    return null;
                int count = VisualTreeHelper.GetChildrenCount(root);
                if (count <= 0)
                    return null;
                List<DependencyObject> child = new List<DependencyObject>();
                for (int i = 0; i < count; i++)
                {
                    DependencyObject c = VisualTreeHelper.GetChild(root, i);
                    if (c != null)
                        child.Add(c);
                }
                return child;
            }

            public static List<DependencyObject> GetDescendants(this DependencyObject root)
            {
                if (root == null)
                    return null;
                List<DependencyObject> temp = root.GetChildern();
                if (temp == null || temp.Count == 0)
                    return null;
                List<DependencyObject> ds = new List<DependencyObject>();          
                ds.AddRange(temp);
                foreach (var t in temp)
                {
                    List<DependencyObject> list = t.GetDescendants();
                    if (list != null && list.Count > 0)
                        ds.AddRange(list);
                }
                return ds;          
            }

  • 相关阅读:
    python爬虫开发与项目实践-学习笔记(一)
    python之TypeError
    学习笔记-python
    python学习之Unable to locate element
    Chrome浏览器之 webdriver(Chrome version must be >= 63.0.3239.0)
    POJ 1830 开关问题 高斯消元
    HDU 4135 Co-prime 容斥原理
    HDU 1796 How many integers can you find 容斥原理
    卡特兰数,组合数,斯特林数,逆元模板
    HDU 6134 Killer Names 数学 斯特林数
  • 原文地址:https://www.cnblogs.com/mjgb/p/2084277.html
Copyright © 2011-2022 走看看