|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- using System.Collections.Generic;
- using System.Windows;
- using System.Windows.Media;
-
- namespace XHZB.Desktop.Utils
- {
- internal class VTHelper
- {
- public static T FindChild<T>(DependencyObject parent, string childName)
- where T : DependencyObject
- {
- if (parent == null)
- {
- return null;
- }
-
- T foundChild = null;
-
- int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
- for (int i = 0; i < childrenCount; i++)
- {
- DependencyObject child = VisualTreeHelper.GetChild(parent, i);
-
- T childType = child as T;
- if (childType == null)
- {
-
- foundChild = FindChild<T>(child, childName);
-
-
- if (foundChild != null)
- {
- break;
- }
- }
- else if (!string.IsNullOrEmpty(childName))
- {
- FrameworkElement frameworkElement = child as FrameworkElement;
-
- if (frameworkElement != null && frameworkElement.Name == childName)
- {
- foundChild = (T)child;
- break;
- }
- }
- else
- {
-
- foundChild = (T)child;
- break;
- }
- }
-
- return foundChild;
- }
-
- public static List<T> FindChilds<T>(DependencyObject parent, string childName)
- where T : DependencyObject
- {
- List<T> list = new List<T>();
- if (parent == null)
- {
- return list;
- }
-
- int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
- for (int i = 0; i < childrenCount; i++)
- {
- DependencyObject child = VisualTreeHelper.GetChild(parent, i);
-
- T childType = child as T;
- if (childType == null)
- {
-
- List<T> findChildList = FindChilds<T>(child, childName);
- for (int j = 0; j < findChildList.Count; j++)
- {
- }
- list.AddRange(FindChilds<T>(child, childName));
- }
- else if (!string.IsNullOrEmpty(childName))
- {
- FrameworkElement frameworkElement = child as FrameworkElement;
-
- if (frameworkElement != null && frameworkElement.Name == childName)
- {
- list.Add((T)child);
- }
- }
- else
- {
-
- list.Add((T)child);
- }
- }
-
- return list;
- }
-
-
-
-
-
-
-
-
- public static T FindParent<T>(DependencyObject i_dp) where T : DependencyObject
- {
- DependencyObject dobj = VisualTreeHelper.GetParent(i_dp);
- if (dobj != null)
- {
- if (dobj is T)
- {
- return (T)dobj;
- }
- else
- {
- dobj = FindParent<T>(dobj);
- if (dobj != null && dobj is T)
- {
- return (T)dobj;
- }
- }
- }
- return null;
- }
- }
- }
|