在需要使用 ListBox 加载大量数据时,可以考虑开启虚拟化,但需要注意的是,虚拟化不一定能开启成功。
以下是一些原因:
- ListBox内部的ScorllViewer不能去掉
如果使用了DataTemplate,需要将其中的ItemsPresenter
用ScrollViewer
包裹起来;
<ControlTemplate TargetType="ListBox"> <Border SnapsToDevicePixels="True"> <ScrollViewer CanContentScroll="True"> <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /> </ScrollViewer> </Border></ControlTemplate>
-
ListBox 外面不能使用 ScrollViewer
-
ListBox 的
ScrollViewer.CanContentScroll ="True"
放ListBox的容器必须是有限高度的,不能是 StackPanel 这样,否则 ScrollViewer 是不会出现哒。
以下是其它的一些参考文章:
获取视觉树中的元素:
ListboxElementName.GetFirstVisualChild<ScrollViewer>();
Accessing the ScrollViewer of a ListBox from C#
原文链接: https://blog.jgrass.cc/posts/listbox-scrollviewer/
本作品采用 「署名 4.0 国际」 许可协议进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。