I have a list box that contains items that are represented by a single textbox.
When the user clicks a button, I want to iterate thru all these text boxes and check if their binding expressions are clean of errors; Should be something like:
Dim errCount = 0
For Each item In MyListBox.ListBoxItems 'There is no such thing ListBoxItems which is actually what I am looking for.
Dim tb As TextBox = item '.........Dig in item to extract the textbox from the visual tree.
errCount += tb.GetBindingExpression(TextBox.TextProperty).HasError
Next
If errCount Then
'Errors found!
End If
Any discussion would be really appreciated. Thanks.
There may be an easier way to do this, but here is one option that will work:
1) Iterate through the list of items.
Because you are using items source,
ListBox.Items
will refer to the data items in the ItemsSource.2) Get the containers for these items.
3) Use VisualTreeHelper to search for a TextBox child of the container visual.
Use this function to search for a visual child of the correct type:
4) Finally, examine the binding on the TextBox.
All put together, something like this:
Translation of previous post to VB:
1)
2)
3)
4)
End Function