WPF ListBox VirtualizingStackPanel.VirtualizationMode=“回收”导致相同的列表项始终出现
我正在使用 WPF/.NET 4,当我将 VirtualizingStackPanel.VirtualizationMode="Recycling" 属性添加到列表框时,它会导致滚动时一遍又一遍地重复相同的列表项。例如,假设我有一个包含 100 个项目的列表框,并且一次可见 10 个项目。当列表框第一次加载时,我看到项目 1 - 10,但是当我开始向下滚动查看剩余项目 11 - 100 时,当我向下滚动时,项目 1 - 10 会一遍又一遍地重复,所以我永远无法查看项目11 - 100。如果我将“回收”更改回“标准”,则一切正常,但滚动速度非常慢(我的 ListItems 相当复杂,其中包含多个图像、文本字段和按钮)。当“回收”打开时,滚动速度非常快,但我只能查看项目 1 - 10。关于问题所在或如何修复它有什么想法吗?提前致谢。
I'm using WPF/.NET 4 and when I add the VirtualizingStackPanel.VirtualizationMode="Recycling" property to my ListBoxes, it causes the same list items to be repeated over and over when scrolling. For example, let's say I have a ListBox with 100 items and 10 are visible at a time. When the ListBox first loads I see items 1 - 10, but when I start scrolling down to see the remaining items 11 - 100, items 1 - 10 are repeated over and over as I scroll down, so I'm never able to view items 11 - 100. If I change Recycling back to Standard, then everything works correctly, but the scrolling is horribly slow (my ListItems are fairly complex, with several images, text fields, and buttons in them). When Recycling is on, the scrolling is super fast, but I can only ever view items 1 - 10. Any ideas on what's wrong or how to fix it? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
顾名思义,回收模式的工作原理是重用渲染的 ListBoxItems。当不再需要 ListBox 项来显示已滚动出列表的项时,ListBox 将重用它来显示已滚动到视图中的项。
要更改 ListBoxItem 显示的内容,ListBox 只需将其 DataContext 属性设置为新项目即可。这要求 ListBoxItem 的内容必须正确响应 DataContextChanged 事件。如果您仅使用数据绑定来填充控件,这将自动发生。但是,如果正如您的评论所暗示的那样,您正在使用一些隐藏代码来更新属性,那么您需要确保您的事件处理代码正在完成这项工作。
Recycling Mode works, as it name implies, by reusing rendered ListBoxItems. When a ListBox item is no longer needed to display an item that has scrolled off the list, the ListBox will reuse it to display one that has scrolled into view.
To change what the ListBoxItem is displaying, the ListBox simply sets its DataContext property to the new item. This requires that the contents of the ListBoxItem have to respond correctly to DataContextChanged events. If you are using only using databinding to populate the controls, this will happen automatically. But if, as your comment implies, you are using some code-behind to update the properties, then you need to make sure that your event handling code is doing the job.