开发者

Accessing TextBoxes in WPF DataBound ListBox with foreach

开发者 https://www.devze.com 2023-01-17 19:45 出处:网络
I have a databound WPF 开发者_如何学运维ListBox with a custom itemtemplate -> datatemplate. Part of that template is also a ListBox.

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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号