Guest User

Untitled

a guest
Apr 25th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.74 KB | None | 0 0
  1. Public Class TreeHelper
  2.  
  3.     Public Shared Function FindChild(Of T As DependencyObject)(parent As DependencyObject, childName As String) As T
  4.         ' Confirm parent and childName are valid.
  5.         If parent Is Nothing Then
  6.             Return Nothing
  7.         End If
  8.  
  9.         Dim foundChild As T = Nothing
  10.  
  11.         Dim childrenCount As Integer = VisualTreeHelper.GetChildrenCount(parent)
  12.         For i As Integer = 0 To childrenCount - 1
  13.             Dim child = VisualTreeHelper.GetChild(parent, i)
  14.             ' If the child is not of the request child type child
  15.             Dim childType As T = TryCast(child, T)
  16.             If childType Is Nothing Then
  17.                 ' recursively drill down the tree
  18.                 foundChild = FindChild(Of T)(child, childName)
  19.  
  20.                 ' If the child is found, break so we do not overwrite the found child.
  21.                 If foundChild IsNot Nothing Then
  22.                     Exit For
  23.                 End If
  24.             ElseIf Not String.IsNullOrEmpty(childName) Then
  25.                 Dim frameworkElement = TryCast(child, FrameworkElement)
  26.                 ' If the child's name is set for search
  27.                 If frameworkElement IsNot Nothing AndAlso frameworkElement.Name = childName Then
  28.                     ' if the child's name is of the request name
  29.                     foundChild = DirectCast(child, T)
  30.                     Exit For
  31.                 End If
  32.             Else
  33.                 ' child element found.
  34.                 foundChild = DirectCast(child, T)
  35.                 Exit For
  36.             End If
  37.         Next
  38.  
  39.         Return foundChild
  40.     End Function
  41.  
  42.     Public Shared Function FindVisualChild(Of childItem As DependencyObject)(ByVal obj As DependencyObject) As childItem
  43.         For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(obj) - 1
  44.             Dim child As DependencyObject = VisualTreeHelper.GetChild(obj, i)
  45.             If child IsNot Nothing AndAlso TypeOf child Is childItem Then
  46.                 Return CType(child, childItem)
  47.             Else
  48.                 Dim childOfChild As childItem = FindVisualChild(Of childItem)(child)
  49.                 If childOfChild IsNot Nothing Then
  50.                     Return childOfChild
  51.                 End If
  52.             End If
  53.         Next i
  54.         Return Nothing
  55.     End Function
  56.  
  57.     Public Shared Function FindVisualChildByNameBeta(Of T As FrameworkElement)(parent As DependencyObject, name As String) As T
  58.         Dim child As T = Nothing
  59.         For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
  60.             Dim ch = VisualTreeHelper.GetChild(parent, i)
  61.             child = TryCast(ch, T)
  62.             If child IsNot Nothing AndAlso child.Name = name Then
  63.                 Exit For
  64.             Else
  65.                 child = FindVisualChildByNameBeta(Of T)(ch, name)
  66.             End If
  67.  
  68.             If child IsNot Nothing Then
  69.                 Exit For
  70.             End If
  71.         Next
  72.         Return child
  73.     End Function
  74.  
  75.     Public Shared Function FindVisualChildByName(ByVal parent As System.Windows.DependencyObject, ByVal Name As String) As System.Windows.DependencyObject
  76.         For i As Integer = 0 To System.Windows.Media.VisualTreeHelper.GetChildrenCount(parent) - 1
  77.             Dim child = System.Windows.Media.VisualTreeHelper.GetChild(parent, i)
  78.             Dim controlName As String = child.GetValue(System.Windows.Controls.Control.NameProperty)
  79.             If controlName = Name Then
  80.                 Return child
  81.             Else
  82.                 Dim res = FindVisualChildByName(child, Name)
  83.                 If Not res Is Nothing Then
  84.                     Return res
  85.                 End If
  86.             End If
  87.         Next
  88.         Return Nothing
  89.     End Function
  90.  
  91. End Class
Advertisement
Add Comment
Please, Sign In to add comment