1. 程式人生 > >WPF中查詢子控制元件、父控制元件的方法

WPF中查詢子控制元件、父控制元件的方法

        /// <summary>
        /// 查詢指定型別的子控制元件
        /// </summary>
        /// <typeparam name="childItem">子控制元件型別</typeparam>
        /// <param name="obj">父控制元件</param>
        /// <returns></returns>
        public static childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
        {
            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 = FindVisualChild<childItem>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }

        /// <summary>
        /// 查詢某種型別的子控制元件,並返回一個List集合
        /// </summary>
        /// <example>List<Button> listButtons = GetChildObjects<Button>(parentPanel, typeof(Button))</example>
        /// <typeparam name="T">子控制元件型別</typeparam>
        /// <param name="obj">父控制元件</param>
        /// <param name="type">子控制元件的類</param>
        /// <returns></returns>
        public static List<T> GetChildObjects<T>(DependencyObject obj, Type type) where T : DependencyObject
        {
            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).GetType() == type))
                {
                    childList.Add((T)child);
                }
                childList.AddRange(GetChildObjects<T>(child, type));
            }
            return childList;
        }

        /// <summary>
        /// 通過名稱查詢子控制元件,並返回一個List集合
        /// </summary>
        /// <example>List<Button> listButtons = GetChildObjects<Button>(parentPanel, "button1")</example>
        /// <typeparam name="T">子控制元件型別</typeparam>
        /// <param name="obj">父控制元件</param>
        /// <param name="name">子控制元件名稱,預設為空</param>
        /// <returns></returns>
        public static List<T> GetChildObjects<T>(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(GetChildObjects<T>(child, name));
            }
            return childList;
        }

        /// <summary>
        /// 通過名稱查詢某型別的子控制元件
        /// </summary>
        /// <example>StackPanel sp = GetChildObject<StackPanel>(this.LayoutRoot, "spDemoPanel")</example>
        /// <typeparam name="T">子控制元件型別</typeparam>
        /// <param name="obj">父控制元件</param>
        /// <param name="name">子控制元件名稱,預設為空</param>
        /// <returns></returns>
        public static T GetChildObject<T>(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 = GetChildObject<T>(child, name);
                    if (grandChild != null)
                        return grandChild;
                }
            }
            return null;
        }

        /// <summary>
        /// 通過名稱查詢父控制元件
        /// </summary>
        /// <example>Grid layoutGrid = VTHelper.GetParentObject<Grid>(this.spDemoPanel, "LayoutRoot")</example>
        /// <typeparam name="T">父控制元件型別</typeparam>
        /// <param name="obj">子控制元件</param>
        /// <param name="name">父控制元件名稱,預設為空</param>
        /// <returns></returns>
        public static T GetParentObject<T>(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;
        }