List children’s components of a WPF control using vb.net

Good morning. Well my weekend was pretty calm. Other than the heartbreaking loss the Packers suffered to the Vikings sunday of course. Truth be told neither team deserved to win that game. But the Packers keep killing themselves with penalties and until they fix that they are not going to be a contender this year.

I began working with WPF a while ago. Though it didn’t come as easily to me as vb.net it is getting better. This enabled all children’s components of a control to be listed.  Nothing complicated but you might find it useful.

Make it a great day!

Class Utility
        Private Shared sbListControls As StringBuilder
        Public Shared Function GetVisualTreeInfo(ByVal element As Visual) As StringBuilder
            If element Is Nothing Then
                Throw New ArgumentNullException(String.Format("Element {0} is Nothing Not ", element.ToString()))
            End If
            sbListControls = New StringBuilder()
            GetControlsList(element, 0)
            Return sbListControls
        End Function
        Private Shared  Sub GetControlsList(ByVal control As Visual, ByVal level As Integer)
            const Integer indent = 4
            Dim ChildNumber As Integer =  VisualTreeHelper.GetChildrenCount(control)
            Dim i As Integer
            For  i = 0 To  ChildNumber – 1 Step  i + 1
                Dim v As Visual = CType(VisualTreeHelper.GetChild(control,i), Visual)
                sbListControls.Append(New String(" "c, level * indent))
                sbListControls.Append(v.GetType())
                sbListControls.Append(Environment.NewLine)
                If VisualTreeHelper.GetChildrenCount(v) > 0 Then
                    GetControlsList(v, level + 1)
                End If
            Next
        End Sub
End Class

 

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: