WPF VirtualizingStackPanel 以提高性能 [英] WPF VirtualizingStackPanel for increased performance
问题描述
我想简单描述一下如何为数据绑定到我的 MVVM 中的 ObservableCollection
的 ItemsControl
实现虚拟化堆栈面板.
I would like a simple description of how to implement a virtualizingstackpanel for an ItemsControl
that is databound to an ObservableCollection
in my MVVM.
我为选项卡控件中的每个选项卡都有一个 ItemsControl
实例,当 ItemsControl
变大时,切换选项卡变得非常慢.
I have an ItemsControl
instance for each tab in a tab control, and switching tabs becomes VERY slow when the ItemsControl
grows larger.
我可以做些什么来加速应用程序?
What can I do to speed up the app?
我打开了一个 WPF 分析器,看到每个选项卡的 ItemsControl 中显示的每个元素(这是一个自定义用户控件)都有自己的 ContentPresenter
.因此,我在 MVVM 中的 ObservableCollection
中基本上有 100 个内容演示者,所有内容都在运行 100 个项目.这是正确的吗?如何优化?
I opened up a WPF profiler and saw that each element (which is a custom user control) displayed in my ItemsControl of each tab had its own ContentPresenter
. So I essentially had 100 content presenters all running for 100 items in my ObservableCollection
in MVVM. Is this corrrect? How can I optimize?
推荐答案
有两种技术可能会有很大帮助.Bea Stolnitz 在她的 博客.
There are two techniques that might be a big help. Both of them are described very well by Bea Stolnitz on her blog.
在 UI 虚拟化中,您可以使用 VirtualizingStackPanel 之类的东西来减少 UI 绘制的内容.
In UI virtualization you use things like VirtualizingStackPanel to make the UI draw fewer things.
数据虚拟化可确保您不会在只显示 100 个对象时将 100 万个对象带入内存.
Data virtualization makes sure you don't bring a million objects into memory when you are only going to show 100.
因此,UI 虚拟化将绘制的事物数量降至最低,而数据虚拟化将可绘制的事物数量降至最低.
So UI virtualization minimizes the number of things drawn and data virtualization minimizes the number of things that could be drawn.
希望有帮助
这篇关于WPF VirtualizingStackPanel 以提高性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!