WPF ListBox 在用户滚动时生成项目
我正在尝试使用 ListBox
向用户显示可能无限的选项列表。目前,我只是在任意点切断列表,但我想允许用户根据需要向下滚动。另外,我想尽可能避免生成不可见的项目,因为必须进行一些计算才能生成每个项目。
我尝试编写 listBox.ItemsSource = enumerable
期望它只向可枚举的可见项询问,但它会尝试读取所有项,如果有无限多个项,则会导致无限循环。
我最好的想法是添加一个侦听器,每当 ListBox
向下滚动时都会收到通知,并添加新项目,以便在最后一个可见项目之后始终有 k
个项目(其中 >k
可能是一次可见的项目数,因此 Page Down 有效)。
有没有一些更干净的方法来处理这个问题?
I am trying to use a ListBox
to display a possibly infinite list of options to a user. Currently, I am simply cutting off the list at an arbitrary point, but I would like to allow the user to scroll down as far as they want. Also, I want to avoid generating non-visible items as much as possible as some computation has to be done to generate each item.
I tried writing listBox.ItemsSource = enumerable
expecting it to only ask the enumerable for visible items, but instead it tries to read all of the items which causes an infinite loop if there are infinitely many items.
My best idea is add a listener that gets notified whenever the ListBox
scrolls down and add new items such that there are always k
more items after the last item visible (where k
is probably the number of items visible at a time so Page Down works).
Is there some cleaner way to handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会使用 VirtualizingStackPanel 来使 UI 绘制更少的元素,然后采用数据虚拟化技术来限制内存中保存的数据。请在此处查看更多详细信息(尤其是此处引用的 Bea Stolnitz 的博客文章)。
I would use a
VirtualizingStackPanel
to make the UI draw fewer elements and then employ a Data Virtualization technique to limit the data you are keeping in memory. See more details here (especially Bea Stolnitz's blog entries referenced here).