潜移默化学会WPF(企业经验篇)--全新替代掉VisualHelper功能的类,可视树泛型查找,功能强大

  1   /// <summary>

  2         /// Finds a Child of a given item in the visual tree. 

  3         /// </summary>

  4         /// <param name="parent">A direct parent of the queried item.</param>

  5         /// <typeparam name="T">The type of the queried item.</typeparam>

  6         /// <param name="childName">x:Name or Name of child. </param>

  7         /// <returns>The first parent item that matches the submitted type parameter. 

  8         /// If not matching item can be found, 

  9         /// a null parent is being returned.</returns>

 10         public static T FindChild<T>(DependencyObject parent, string childName)

 11            where T : DependencyObject

 12         {

 13             // Confirm parent and childName are valid. 

 14             if (parent == null) return null;

 15 

 16             T foundChild = null;

 17 

 18             int childrenCount = VisualTreeHelper.GetChildrenCount(parent);

 19             for (int i = 0; i < childrenCount; i++)

 20             {

 21                 var child = VisualTreeHelper.GetChild(parent, i);

 22                 // If the child is not of the request child type child

 23                 T childType = child as T;

 24                 if (childType == null)

 25                 {

 26                     // recursively drill down the tree

 27                     foundChild = FindChild<T>(child, childName);

 28 

 29                     // If the child is found, break so we do not overwrite the found child. 

 30                     if (foundChild != null) break;

 31                 }

 32                 else if (!string.IsNullOrEmpty(childName))

 33                 {

 34                     var frameworkElement = child as FrameworkElement;

 35                     // If the child's name is set for search

 36                     if (frameworkElement != null && frameworkElement.Name == childName)

 37                     {

 38                         // if the child's name is of the request name

 39                         foundChild = (T)child;

 40                         break;

 41                     }

 42                 }

 43                 else

 44                 {

 45                     // child element found.

 46                     foundChild = (T)child;

 47                     break;

 48                 }

 49             }

 50 

 51             return foundChild;

 52         }

 53 

 54         public DependencyObject FindChild(DependencyObject o, Type childType)

 55         {

 56             DependencyObject foundChild = null;

 57             if (o != null)

 58             {

 59                 int childrenCount = VisualTreeHelper.GetChildrenCount(o);

 60                 for (int i = 0; i < childrenCount; i++)

 61                 {

 62                     var child = VisualTreeHelper.GetChild(o, i);

 63                     if (child.GetType() != childType)

 64                     {

 65                         foundChild = FindChild(child, childType);

 66                     }

 67                     else

 68                     {

 69                         foundChild = child;

 70                         break;

 71                     }

 72                 }

 73             }

 74             return foundChild;

 75         }

 76 

 77         public static T FindVisualParent<T>(UIElement element) where T : UIElement

 78         {

 79             UIElement parent = element; while (parent != null)

 80             {

 81                 T correctlyTyped = parent as T; if (correctlyTyped != null)

 82                 {

 83                     return correctlyTyped;

 84                 }

 85                 parent = VisualTreeHelper.GetParent(parent) as UIElement;

 86             } return null;

 87         }

 88 

 89         public static T FindChild<T>(DependencyObject parent) where T : DependencyObject

 90         {

 91             if (parent == null) return null;

 92 

 93             T childElement = null; int childrenCount = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < childrenCount; i++)

 94             {

 95                 var child = VisualTreeHelper.GetChild(parent, i);

 96                 T childType = child as T; if (childType == null)

 97                 {

 98                     childElement = FindChild<T>(child); if (childElement != null) break;

 99                 }

100                 else

101                 {

102                     childElement = (T)child; break;

103                 }

104             } return childElement;

105         }

用法,你懂的

你可能感兴趣的:(help)