• WPF silverlight获取子控件(获取DataTemplate里的子控件)


    public static class VisualTreeExtensions
        {
            /// <summary>
            /// 获取父节点控件
            /// </summary>
            /// <typeparam name="T">子控件UI</typeparam>
            /// <param name="obj">子控件</param>
            /// <param name="name">父容器名称</param>
            /// <returns>父容器对象</returns>
            public static T GetParent<T>(this DependencyObject obj, string name = null) where T : FrameworkElement
            {
                DependencyObject parent = VisualTreeHelper.GetParent(obj);
    
                while (parent != null)
                {
                    if (parent is T && (((T)parent).Name == name || string.IsNullOrEmpty(name)))
                    {
                        return (T)parent;
                    }
                    parent = VisualTreeHelper.GetParent(parent);
                }
    
                return null;
            }
            /// <summary>
            /// 获取子控件
            /// </summary>
            /// <typeparam name="T">UI类型</typeparam>
            /// <param name="obj">父容器对象</param>
            /// <param name="name">子控件名称</param>
            /// <returns>子控件</returns>
            public static T GetChild<T>(this DependencyObject obj, string name = null) 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 = GetChild<T>(child, name);
                        if (grandChild != null)
                            return grandChild;
                    }
                }
                return null;
            }
            /// <summary>
            /// 获取子控件集合
            /// </summary>
            /// <typeparam name="T">UI类型</typeparam>
            /// <param name="obj">父容器对象</param>
            /// <param name="name">子控件名称</param>
            /// <returns>子控件集合</returns>
            public static List<T> GetChildren<T>(this DependencyObject obj, string name = null) 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 && (((T)child).Name == name || string.IsNullOrEmpty(name)))
                    {
                        childList.Add((T)child);
                    }
                    childList.AddRange(GetChildren<T>(child, ""));
                }
                return childList;
            }
    
            /// <summary>
            /// 查找某控件下的所有給定類型的子控件集合
            /// </summary>
            /// <typeparam name="T">要查找的子控件類型</typeparam>
            /// <param name="depObj">父控件</param>
            /// <returns>子控件集合</returns>
            public static T FindVisualChild<T>(this DependencyObject depObj, string childName = null) where T : FrameworkElement
            {
                if (depObj != null)
                {
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                    {
                        DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                        if (child != null && child is T &&
                            (((T)child).Name == childName || string.IsNullOrEmpty(childName)))
                        {
                            return (T)child;
                        }
                        else
                        {
                            T childOfChild = FindVisualChild<T>(child, childName);
                            if (childOfChild != null && childOfChild is T)
                            {
                                return childOfChild;
                            }
                        }
                    }
                    return null;
                }
                return null;
            }
    
    
            /// <summary>
            /// 查找某控件下的所有給定類型的子控件集合
            /// </summary>
            /// <typeparam name="T">要查找的子控件類型</typeparam>
            /// <param name="depObj">父控件</param>
            /// <returns>子控件集合</returns>
            public static List<T> FindVisualChildren<T>(this DependencyObject depObj, string childName = null) where T : FrameworkElement
            {
                List<T> list = new List<T>();
                if (depObj != null)
                {
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                    {
                        DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                        if (child != null && child is T
                            && (((T)child).Name == childName || string.IsNullOrEmpty(childName)))
                        {
                            list.Add((T)child);
                        }
    
                        List<T> childItems = FindVisualChildren<T>(child, childName);
                        if (childItems != null && childItems.Count > 0)
                        {
                            foreach (var item in childItems)
                            {
                                list.Add(item);
                            }
                        }
                    }
                }
                return list;
            }
        }
  • 相关阅读:
    c/c++ -->对象和类
    zynq基础-->LINUX 设备树
    zynq基础-->yocto
    python3-->基础操作
    构建之法-->概论
    zynq基础-->linux下软件应用
    zynq基础-->系统构架
    网络基础-->http协议
    (转)频谱仪测试pll锁定时间
    modelsim仿真
  • 原文地址:https://www.cnblogs.com/akiing/p/4029200.html
Copyright © 2020-2023  润新知