Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class TreeHelper
- Public Shared Function FindChild(Of T As DependencyObject)(parent As DependencyObject, childName As String) As T
- ' Confirm parent and childName are valid.
- If parent Is Nothing Then
- Return Nothing
- End If
- Dim foundChild As T = Nothing
- Dim childrenCount As Integer = VisualTreeHelper.GetChildrenCount(parent)
- For i As Integer = 0 To childrenCount - 1
- Dim child = VisualTreeHelper.GetChild(parent, i)
- ' If the child is not of the request child type child
- Dim childType As T = TryCast(child, T)
- If childType Is Nothing Then
- ' recursively drill down the tree
- foundChild = FindChild(Of T)(child, childName)
- ' If the child is found, break so we do not overwrite the found child.
- If foundChild IsNot Nothing Then
- Exit For
- End If
- ElseIf Not String.IsNullOrEmpty(childName) Then
- Dim frameworkElement = TryCast(child, FrameworkElement)
- ' If the child's name is set for search
- If frameworkElement IsNot Nothing AndAlso frameworkElement.Name = childName Then
- ' if the child's name is of the request name
- foundChild = DirectCast(child, T)
- Exit For
- End If
- Else
- ' child element found.
- foundChild = DirectCast(child, T)
- Exit For
- End If
- Next
- Return foundChild
- End Function
- Public Shared Function FindVisualChild(Of childItem As DependencyObject)(ByVal obj As DependencyObject) As childItem
- For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(obj) - 1
- Dim child As DependencyObject = VisualTreeHelper.GetChild(obj, i)
- If child IsNot Nothing AndAlso TypeOf child Is childItem Then
- Return CType(child, childItem)
- Else
- Dim childOfChild As childItem = FindVisualChild(Of childItem)(child)
- If childOfChild IsNot Nothing Then
- Return childOfChild
- End If
- End If
- Next i
- Return Nothing
- End Function
- Public Shared Function FindVisualChildByNameBeta(Of T As FrameworkElement)(parent As DependencyObject, name As String) As T
- Dim child As T = Nothing
- For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
- Dim ch = VisualTreeHelper.GetChild(parent, i)
- child = TryCast(ch, T)
- If child IsNot Nothing AndAlso child.Name = name Then
- Exit For
- Else
- child = FindVisualChildByNameBeta(Of T)(ch, name)
- End If
- If child IsNot Nothing Then
- Exit For
- End If
- Next
- Return child
- End Function
- Public Shared Function FindVisualChildByName(ByVal parent As System.Windows.DependencyObject, ByVal Name As String) As System.Windows.DependencyObject
- For i As Integer = 0 To System.Windows.Media.VisualTreeHelper.GetChildrenCount(parent) - 1
- Dim child = System.Windows.Media.VisualTreeHelper.GetChild(parent, i)
- Dim controlName As String = child.GetValue(System.Windows.Controls.Control.NameProperty)
- If controlName = Name Then
- Return child
- Else
- Dim res = FindVisualChildByName(child, Name)
- If Not res Is Nothing Then
- Return res
- End If
- End If
- Next
- Return Nothing
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment