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; } }