WPF VirtualizingStackPanel 以提高性能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1389769/
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 20:57:56  来源:igfitidea点击:

WPF VirtualizingStackPanel for increased performance

wpfperformancevirtualizationstackpanel

提问by bluebit

I would like a simple description of how to implement a virtualizingstackpanel for an ItemsControlthat is databound to an ObservableCollectionin my MVVM.

我想要一个关于如何ItemsControl为数据绑定到ObservableCollection我的 MVVM 中的数据实现虚拟化堆栈面板的简单描述。

I have an ItemsControlinstance for each tab in a tab control, and switching tabs becomes VERY slow when the ItemsControlgrows larger.

我有一个ItemsControl选项卡控件中的每个选项卡的实例,当ItemsControl变大时,切换选项卡变得非常慢。

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 ObservableCollectionin MVVM. Is this corrrect? How can I optimize?

我打开了一个 WPF 分析器,看到每个选项卡的 ItemsControl 中显示的每个元素(这是一个自定义用户控件)都有自己的ContentPresenter. 所以我基本上有 100 个内容演示者在我ObservableCollection的 MVVM 中运行 100 个项目。这是正确的吗?我该如何优化?

回答by Mike Two

There are two techniques that might be a big help. Both of them are described very well by Bea Stolnitz on her blog.

有两种技术可能会有很大帮助。Bea Stolnitz 在她的博客上对这两者进行了很好的描述。

The first is UI Virtualizationand the second is Data Virtualization

第一个是UI 虚拟化,第二个是数据虚拟化

In UI virtualization you use things like VirtualizingStackPanel to make the UI draw fewer things.

在 UI 虚拟化中,您使用 VirtualizingStackPanel 之类的东西来使 UI 绘制更少的东西。

Data virtualization makes sure you don't bring a million objects into memory when you are only going to show 100.

数据虚拟化可确保您不会在只显示 100 个对象时将 100 万个对象带入内存。

So UI virtualization minimizes the number of things drawn and data virtualization minimizes the number of things that could be drawn.

因此,UI 虚拟化最大限度地减少了绘制的东西的数量,而数据虚拟化则最大限度地减少了可以绘制的东西的数量。

Hope that helps

希望有帮助

回答by user227789

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

我在使用 TabControl 和 DataGrid 的 WPF 中遇到了完全相同的问题。通过增加 DataGrid 元素的大小,切换选项卡变得非常缓慢!之后,我发现这篇文章阅读了 Bea Stolnitz 的博客,正如上一个答案所假设的那样。这给了我谷歌“wpf tabcontrol VirtualizingStackPanel”的提示,它给了我到 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. ....

.... 性能命中是在树的构建过程中。不幸的是,如果
您使用典型的 MVVM 方法并绑定
TabControl的 ItemsSource属性,则每次
选择选项卡项时都必须重新构建整个树。这通常是非常昂贵的操作。....