【问题标题】:Accessing TextBoxes in WPF DataBound ListBox with foreach使用 foreach 访问 WPF DataBound ListBox 中的文本框
【发布时间】:2010-09-28 23:15:41
【问题描述】:

我有一个带有自定义项目模板的数据绑定 WPF 列表框 -> 数据模板。该模板的一部分也是一个列表框。 在某些事件中,我想遍历 ListBox 中的所有文本框并检索它们的值。 这可能吗?

【问题讨论】:

    标签: wpf data-binding


    【解决方案1】:

    您应该能够通过使用ItemContainerGenerator 并在模板中查找元素来做到这一点:

    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;
    }
    

    (如果您引用的文本框在模板中的 ListBox 中,您必须通过重复相同的方法来深入挖掘。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多