WPF 访问列表框内的项目,该列表框具有 UserControl 作为 ItemTemplate
- 我的窗口有一个 ListBox ListBox
- (MyListBox) 有一个 DataTable 其 DataContext
- ListBox 的 ItemSource 是: {Binding}
- Listbox 有一个 UserControl(MyUserControl) 作为 DataTemplate
- UserControl 有 RadioButtons 和 TextBoxes (首先它们填充了来自 DataTable 和的值然后用户可以更改它们)
- 窗口有一个提交按钮
我想要做的是,当用户单击提交按钮时,
- 对于每个列表框项,从用户控件的文本框和单选按钮中获取值。
我在这项工作中使用了这种方法:
foreach(var element in MyListBox.Items)
{
var border = MyListBox.ItemContainerGenerator.ContainerFromItem(element)as FrameworkElement;
MyUserControl currentControl = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(myBorder,0) as Border,0)as ContentPresenter,0)as MyUserControl;
//And use currentControl
}
当在列表框中使用 3-5 个项目时,我什么也没意识到。但是当我使用更多的项目时,我发现在 foreach 函数中循环一些元素后,“var border”变为“null”。
我在这里找到了原因: ListView.ItemContainerGenerator.ContainerFromItem(item) 在 20 个项目后返回 null
那么我现在能做什么呢?我想访问所有项目并在用户控件上获取它们的值。
谢谢
- I have Window that has a ListBox
- ListBox(MyListBox) has a DataTable for its DataContext
- ListBox's ItemSource is : {Binding}
- Listbox has a UserControl(MyUserControl) as DataTemplate
- UserControl has RadioButtons and TextBoxes (At first They're filled with values from DataTable and then user can change them)
- Window has one Submit Button
What I want to do is, when user clicks the submit button
- For each ListBox Item, get the values form UserControl's TextBoxes and RadioButtons.
I was using that method for this job :
foreach(var element in MyListBox.Items)
{
var border = MyListBox.ItemContainerGenerator.ContainerFromItem(element)as FrameworkElement;
MyUserControl currentControl = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(myBorder,0) as Border,0)as ContentPresenter,0)as MyUserControl;
//And use currentControl
}
I realised nothing when using 3-5 items in Listbox. But when I used much more items, I saw that "var border" gets "null" after some elements looped in foreach function.
I found the reason here :
ListView.ItemContainerGenerator.ContainerFromItem(item) return null after 20 items
So what can I do now? I want to access all items and get their values sitting on user controls.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用实现
INotifyPropertyChanged
的对象,并将其ObservableCollection
绑定到ItemSource
然后你就可以得到所有的项目列表。
这里有一些来自 MSDN 的快速链接,可获取更多信息
如何:实现属性更改通知
绑定源概述
您应该在 google 上搜索一些有关此内容的教程。
You should use objects who implement
INotifyPropertyChanged
and bind anObservableCollection
of it to theItemSource
And then you can get all the list of items.
Here some quick links from MSDN to get more informations
How to: Implement Property Change Notification
Binding Sources Overview
You should google for some tutorials about this.
Zied 的帖子就是这个问题的解决方案。但我为我的项目做了以下操作:
我删除了
MyListBox.DataContext = myDataTable
并使用了这个:<块引用>
<块引用>
容易吧? :)
Zied's post is a solution for this problem. But I did the following for my project:
I removed
MyListBox.DataContext = myDataTable
and used this:Easy huh? :)