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
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
Loading
Robert HurdPosted Mar 28, 2011, 8:43 PM
I've managed to crack it!! I remember having an example program from one of my WPF books that creates a visual tree of any element on a page / window. By hacking it around I can use the VisualTreeHelper to find my TextBox.
I've attached my code. See Inspection.xaml.vb for the looping code
The main bit of code that has done the trick is:
ahhhhhh don't rub it in!! haha. When I was at university I used to live in Bristol, UK, and I used to get a 20Mbps cable connection! The good days! I think they are up to 50Mbs now.
I've managed to crack it!! I remember having an example program from one of my WPF books that creates a visual tree of any element on a page / window. By hacking it around I can use the VisualTreeHelper to find my TextBox.
I've attached my code. See Inspection.xaml.vb for the looping code
The main bit of code that has done the trick is:
Private
Sub ProcessElement(ByVal element As DependencyObject, ByVal listTabItem As TabItem)'Check if element is a TextBox
If element.GetType().FullName = GetType(TextBox).FullName Then'***TEXTBOX FOUND!!***
End If
' Check if this element contains other elements.
For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(element) - 1
' Process each contained element recursively.
ProcessElement(VisualTreeHelper.GetChild(element, i), listTabItem)
Next
End Sub
Thank you for input Frogleg! It's greatly appreciated!
FroglegPosted Mar 28, 2011, 6:38 PM
If you can explain the part of your project so that I can create the same, I may be able to help
Robert HurdPosted Mar 28, 2011, 6:24 PM
A lot of it is also business classes for SQL Database queries etc.
The only files of concern is just the 4 files that I've uploaded (a XAML page containing the TabControl, and the UserControl XAML file).
FroglegPosted Mar 28, 2011, 2:49 PM
Try and upload it
Robert HurdPosted Mar 25, 2011, 7:33 PM
I've only included the files in question from my project as the overall project is quite large.
Thanks.
FroglegPosted Mar 25, 2011, 5:30 PM