1 public static List<T> GetChildObj<T>(DependencyObject obj) where T : FrameworkElement 2 { 3 List<T> list = new List<T>(); 4 DependencyObject child = null; 5 for (int i = 0; i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(obj); i++) 6 { 7 child = System.Windows.Media.VisualTreeHelper.GetChild(obj, i); 8 if (child is T) 9 { 10 list.Add((T)child); 11 } 12 //GetChildObj<T>(child);//仅仅遍历只在当前控件内的元素 13 list.AddRange(GetChildObj<T>(child));//遍历当前控件包括所有子控件 14 } 15 16 return list; 17 }
时间: 2024-10-18 09:24:05