wpf 为什么将 ScrollViewer.CanContentScroll 设置为 false 禁用虚拟化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3724593/
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
why setting ScrollViewer.CanContentScroll to false disable virtualization
提问by Park Wu
As most WPF developers know, setting ScrollViewer.CanContentScroll
to false
will disable virtualization; but I'd like to know how it works, because I try to enable virtualization while setting ScrollViewer.CanContentScroll
to false
.
大多数 WPF 开发人员都知道,设置ScrollViewer.CanContentScroll
为false
将禁用虚拟化;但我想知道它是如何工作的,因为我尝试在设置ScrollViewer.CanContentScroll
为false
.
回答by rudigrobler
"ScrollViewer currently allows two scrolling modes: smooth pixel-by-pixel scrolling (CanContentScroll = false) or discrete item-by-item scrolling (CanContentScroll = true). Currently WPF supports UI virtualization only when scrolling by item. Pixel-based scrolling is also called “physical scrolling” and item-based scrolling is also called “logical scrolling”."
"ScrollViewer 目前允许两种滚动模式:平滑逐像素滚动(CanContentScroll = false)或离散逐项滚动(CanContentScroll = true)。目前 WPF 仅在逐项滚动时支持 UI 虚拟化。基于像素的滚动是也称为“物理滚动”,基于项目的滚动也称为“逻辑滚动”。”
Virtualization requires an item-based scrolling so it can keep track of logical units (items) currently in view... Setting the ScrollViewer to a pixel-based scrolling there is no more concept of logic units but only pixels!!!
虚拟化需要基于项目的滚动,因此它可以跟踪当前视图中的逻辑单元(项目)...将 ScrollViewer 设置为基于像素的滚动,不再有逻辑单元的概念,只有像素!!!
回答by egoroveo
I'm often asked if there is a way to work around this limitation. Well, anything is possible, but there is no easyworkaround. You would have to re-implement significant portions of the current virtualization logic to combine pixel-based scrolling with UI virtualization. You would also have to solve some interesting problems that come with it. For example, how do you calculate the size of the thumb when the item containers have different heights? (Remember that you don't know the height of the virtualized containers – you only know the height of the containers that are currently displayed.) You could assume an average based on the heights you do know, or you could keep a list with the item heights as items are brought into memory (which would increase accuracy of the thumb size as the user interacts with the control). You could also decide that pixel-based scrolling only works with items that are of the same height – this would simplify the solution. So, yes, you could come up with a solution to work around this limitation, but it's not trivial.
我经常被问到是否有办法解决这个限制。嗯,一切皆有可能,但没有那么容易解决方法。您将不得不重新实现当前虚拟化逻辑的重要部分,以将基于像素的滚动与 UI 虚拟化相结合。您还必须解决一些随之而来的有趣问题。例如,当物品容器具有不同的高度时,如何计算拇指的大小?(请记住,您不知道虚拟化容器的高度 - 您只知道当前显示的容器的高度。)您可以根据您知道的高度假设一个平均值,或者您可以保留一个列表项目被带入内存时的项目高度(当用户与控件交互时,这将增加拇指大小的准确性)。您还可以决定基于像素的滚动仅适用于高度相同的项目——这将简化解决方案。所以,
回答by Hoddmimes
You can restore the virtualization with VirtualizingPanel.ScrollUnit="Pixel"
(in .NET >= 4.5).
您可以使用VirtualizingPanel.ScrollUnit="Pixel"
(在 .NET >= 4.5 中)恢复虚拟化。