如何在wp7中动态添加listbox.itemsource中的元素?
我有一个带有数据模板的列表框,有 2 个文本块,它们绑定某个类的某些属性,我有一个包含该类类型的元素的列表,我想动态显示此元素,即首先显示前 20 个元素,然后显示下一个 20 个元素等,但是我该怎么做,对于 itemsource 我只能分配一个列表,我无法动态向其添加元素,请帮助我。
I have a listbox with datatemplate, with 2 textblocks which binding some properties of some class,i have a list with elements of type of that class,i want to dynamically show this elements i.e at first show first 20 elements, than next 20 and etc, but how can I do that, to itemsource I can only assign a list, I can't add elements to it dynamically, please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使
ListBox
在将项目添加到列表时动态更新,您需要选择一个在添加/删除项目时引发事件的集合类。您应该使用ObservableCollection
作为您的ItemsSource
。该集合在修改时会引发CollectionChanged
事件。ListBox
处理此事件并相应更新。In order for a
ListBox
to update dynamically as items are added to the list, you need to choose a collection class that raises events when items are added / removed. You should useObservableCollection
as yourItemsSource
. This collection raises theCollectionChanged
event when it is modified.ListBox
handles this event and updates accordingly.