WPF 中的 ListBox、VirtualizingStackPanel 和平滑滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1924089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:21:04  来源:igfitidea点击:

ListBox, VirtualizingStackPanel, and Smooth Scrolling in WPF

wpfperformancelistboxscrollvirtualizingstackpanel

提问by Joel Cochran

I have a ListBoxthat may have many rows of templated DB records, including an Image, bound to an ObservableCollection<MyItem>. Sometimes the collection could hold thousands of items.

我有一个ListBox可能有很多行模板化数据库记录,包括Image绑定到ObservableCollection<MyItem>. 有时,该系列可以容纳数千件物品。

The performance is great, but the scrolling is the default jumpy behavior. I would like it to have smooth scrolling, so I unchecked ScrollViewer.CanContentScroll.

性能很棒,但滚动是默认的跳跃行为。我希望它能够平滑滚动,所以我取消了ScrollViewer.CanContentScroll.

Now I have smooth scrolling, but the performance is horrendous: the data is retrieved in a separate thread, and the thread finishes quickly, but it takes 10-20 seconds for the results to show in the ListBox. I assume that this is because unchecking ScrollViewer.CanContentScrollchanges the underlying VirtualizingStackPanelto a regular StackPaneland so it is loading the entire collection before displaying the results.

现在我有了平滑的滚动,但性能却很糟糕:数据是在一个单独的线程中检索的,线程很快完成,但结果需要 10-20 秒才能显示在ListBox. 我认为这是因为取消选中ScrollViewer.CanContentScroll将底层更改VirtualizingStackPanel为常规StackPanel,因此它在显示结果之前加载整个集合。

So my question is this: how do I retain the the smooth scrolling without sacrificing the VirtualizingStackPanelbehavior and performance?

所以我的问题是:如何在不牺牲VirtualizingStackPanel行为和性能的情况下保持平滑滚动?

采纳答案by Anvaka

When you uncheck CanContentScroll, you lose virtualization. And the answer is really frustrating: For now there is no out-of-the-box solution :(.

当您取消选中 CanContentScroll 时,您将失去虚拟化。答案确实令人沮丧:目前还没有现成的解决方案:(。

PS: This is not the first post here, asking this very question.

PS:这不是这里的第一篇文章,问这个问题

回答by Zodman

If you use .NET 4.5 (or 4.0 if you're willing to hack a bit) then there's an answer over here.

如果你使用.NET 4.5(或4.0,如果你愿意砍了一下),然后还有一个答案在这里

[Note that @Guilluame's comment was here way before this answer but it wasn't particularly visible when skimming for answers.]

[请注意,@Guillame 的评论在此答案之前就在这里,但在浏览答案时并不是特别明显。]