• WPF XMAL获取元素的父元素,子元素


    /// 获得指定元素的父元素  
            /// </summary>  
            /// <typeparam name="T">指定页面元素</typeparam>  
            /// <param name="obj"></param>  
            /// <returns></returns>  
            public T GetParentObject<T>(DependencyObject obj) where T : FrameworkElement  
            {  
                DependencyObject parent = VisualTreeHelper.GetParent(obj);  
      
                while (parent != null)  
                {  
                    if (parent is T)  
                    {  
                        return (T)parent;  
                    }  
      
                    parent = VisualTreeHelper.GetParent(parent);  
                }  
      
                return null;  
            }  
      
            /// <summary>  
            /// 获得指定元素的所有子元素  
            /// </summary>  
            /// <typeparam name="T"></typeparam>  
            /// <param name="obj"></param>  
            /// <returns></returns>  
            public List<T> GetChildObjects<T>(DependencyObject obj) where T : FrameworkElement  
            {  
                DependencyObject child = null;  
                List<T> childList = new List<T>();  
      
                for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)  
                {  
                    child = VisualTreeHelper.GetChild(obj, i);  
      
                    if (child is T)  
                    {  
                        childList.Add((T)child);  
                    }  
                    childList.AddRange(GetChildObjects<T>(child));  
                }  
                return childList;  
            }  
    

      

     /// 查找子元素  
            /// </summary>  
            /// <typeparam name="T"></typeparam>  
            /// <param name="obj"></param>  
            /// <param name="name"></param>  
            /// <returns></returns>  
            public T GetChildObject<T>(DependencyObject obj, string name) where T : FrameworkElement  
            {  
                DependencyObject child = null;  
                T grandChild = null;  
      
      
                for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)  
                {  
                    child = VisualTreeHelper.GetChild(obj, i);  
      
      
                    if (child is T && (((T)child).Name == name | string.IsNullOrEmpty(name)))  
                    {  
                        return (T)child;  
                    }  
                    else  
                    {  
                        grandChild = GetChildObject<T>(child, name);  
                        if (grandChild != null)  
                            return grandChild;  
                    }  
                }  
                return null;  
            }  
    

      转载自http://blog.csdn.net/duanzi_peng/article/details/17094639

  • 相关阅读:
    11个重要的数据库设计规则
    CentOS 6.8 新安装系统的网络IP配置(转载)
    WebView根据加载的内容来控制其高度
    遗传算法
    Selenium: Trying to log in with cookies and get the errorMessage
    用Tesseract训练验证码遇到的问题
    利用jTessBoxEditor工具进行Tesseract-OCR样本训练
    Tesseract处理背景渐变的图片
    XPath语法
    在Python中用Selenium执行JavaScript
  • 原文地址:https://www.cnblogs.com/lelehellow/p/6699424.html
Copyright © 2020-2023  润新知