Accessing TextBoxes in WPF DataBound ListBox with foreach
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?
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.)
精彩评论