0
votes

I have a databound WPF ListBox with a custom itemtemplate -> datatemplate. Part of that template is also a ListBox. On certain event I'd like to loop through all textboxes in the ListBox and retreive their values. Is this possible?

1

1 Answers

1
votes

You should be able to do this by using the ItemContainerGenerator and finding elements in the template:

foreach (var item in lb.Items)
{
    var itemContainer = lb.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
    // Name the TextBox in the template to find it here.
    var textBox = itemContainer.ContentTemplate.FindName("?????", itemContainer) as TextBox; 
    var value = textBox.Text;
}

(If the TextBoxes you referred to are within the ListBox which is in the template you have to dig deeper by repeating the same method.)