Robert Hurd

Robert Hurd

  • NA
  • 10
  • 22.3k

WPF Vb.net How to loop through ListBoxItems to find a nested control?

Mar 24 2011 9:58 PM
Hi everyone,

Firstly, thank you for taking the time out to read this post.

I'm having trouble trying to loop through a WPF ListBox control to find a nested TextBox in VB.net 3.5.

Here is my UI structure:

TabControl > TabItem > UserControl > Grid > ListBox > ListBoxItem > Grid > TextBox

I have a page that Contains a TabControl. When the TabControl loads, I loop through each TabItem to dynamically add a UserControl which contains a grid, where the first grid row contains a ListBox. In each ListBox I dynamically add 8 ListItems, where each item is DataTemplate that contains another Grid that houses an Image, TextBox and Button.

I need to loop through all of the items to find the textbox nested in each ListBoxItem. I've managed to drill down the the ListBoxItem level but I don't know how to dig further down to find the TextBox.

Here is my VB code so far:


'Loop through TabItems in TabControl
For tabItems As Integer = 0 To tabInspection.Items.Count - 1
        'Create object of the current TabItem
        Dim tabItem As TabItem = tabInspection.Items(tabItems)
        'Continue if TabItem Header is not Save and Exit
        If tabItem.Header <> "Save and Exit" Then
                'Find ListBox that is nested in UserControl, Grid, TabControl, TabItem
                For Each ctrl As Control In tabItem.Content.Content.Children
                        'Check if control found is a ListBox
                        If ctrl.GetType().FullName = GetType(ListBox).FullName Then
                                'Create Control object of the ListBox
                                Dim lstBox As ListBox = CType(ctrl, ListBox)
                                'Loop Through ListBox items
                                For Each item In lstBox.Items
                                        MessageBox.Show(item.ToString,
"List Box Item")
                                        ' *** NEED TO FIND TEXTBOX CONTROL NESTED IN A LISTBOX ITEM > GRID ***
                                Next
                        End If
                Next
        End If
Next


Any advice is greatly appreciated!

Thank you again for your time,

Rob