WPF VirtualizingStackPanel 可提高性能
我想要一个关于如何为 ItemsControl
实现 virtualizingstackpanel 的简单描述,该 ItemsControl
数据绑定到我的 MVVM 中的 ObservableCollection
。
我在选项卡控件中的每个选项卡都有一个 ItemsControl
实例,当 ItemsControl
变大时,切换选项卡会变得非常慢。
我可以做什么来加快应用程序的速度?
我打开了一个 WPF 分析器,发现每个选项卡的 ItemsControl 中显示的每个元素(这是一个自定义用户控件)都有自己的 ContentPresenter
。因此,我基本上有 100 个内容呈现器,全部运行在 MVVM 中的 ObservableCollection
中的 100 个项目。这是正确的吗?我该如何优化?
I would like a simple description of how to implement a virtualizingstackpanel for an ItemsControl
that is databound to an ObservableCollection
in my MVVM.
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?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两种技术可能会有很大帮助。 Bea Stolnitz 在她的
第一个是UI虚拟化
第二个是数据虚拟化
在 UI 虚拟化中,您可以使用 VirtualizingStackPanel 之类的东西来使 UI 绘制更少的东西。
数据虚拟化可确保您在只显示 100 个对象时不会将 100 万个对象放入内存中。
因此 UI 虚拟化可最大限度地减少绘制的对象数量,而数据虚拟化则可最大限度地减少可绘制的对象数量。
希望有帮助
There are two techniques that might be a big help. Both of them are described very well by Bea Stolnitz on her blog.
The first is UI Virtualization
and the second is Data Virtualization
In UI virtualization you use things like VirtualizingStackPanel to make the UI draw fewer things.
Data virtualization makes sure you don't bring a million objects into memory when you are only going to show 100.
So UI virtualization minimizes the number of things drawn and data virtualization minimizes the number of things that could be drawn.
Hope that helps
我在使用 TabControl 和 DataGrid 的 WPF 中遇到了完全相同的问题。通过增加 DataGrid 元素大小,切换选项卡变得非常慢!之后,我发现这篇文章阅读了 Bea Stolnitz 的博客,正如之前的答案所假设的那样。这给了我谷歌“wpf tabcontrol VirtualizingStackPanel”的提示
这给了我 DrWPF 的链接: http://groups.google.com /group/wpf-disciples/browse_thread/thread/6f3531a1720252dd
他准确地描述了问题并给出了解决方案:-))
.... 性能命中是在树的构建期间。不幸的是,如果
您正在使用典型的 MVVM 方法并绑定到 ItemsSource
TabControl 的属性,每次都必须重建整个树
选择了一个选项卡项目。这通常是一项非常昂贵的操作。 ....
I had exact the same problem ind WPF using TabControl and DataGrid. By growing DataGrid element size, switching tabs becomes VERY slow ! After that I found this post reading the blog from Bea Stolnitz as supposed by the previous answer. That gave me the hint to google "wpf tabcontrol VirtualizingStackPanel"
which gives me the link to DrWPF: http://groups.google.com/group/wpf-disciples/browse_thread/thread/6f3531a1720252dd
He describes exactly the problem and gives the solution :-))
.... The perf hit is during the building of the tree. Unfortunately, if
you're using a typical MVVM approach with a binding on the ItemsSource
property of the TabControl, the entire tree must be rebuilt each time
a tab item is selected. This is usually a very expensive operation. ....