C# 提高 WPF DataGrid 性能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13764579/
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
Improve WPF DataGrid performance
提问by Kishor
In my .NET 3.5WPFApplication, I have a WPFDataGridwhich will be populated with 500 columns and 50 rows.
The performance of App is very very poor in scrolling, or when I do DataGrid.Items.Refresh()or in selecting rows.
在我的.NET 3.5WPF应用程序中,我有一个WPFDataGrid将填充 500 列和 50 行。App 在滚动时的性能非常差,或者当我这样做DataGrid.Items.Refresh()或选择行时。
Actually App will take around 20 sec to Update Layout. Layout_Updated()event will trigger after 20 sec.
实际上 App 需要大约 20 秒来更新布局。Layout_Updated()事件将在 20 秒后触发。
If I reduce the columns to 50 or less, App will be very responsive. As per my findings performance is directly related to column count.
如果我将列减少到 50 或更少,应用程序将非常敏感。根据我的发现,性能与列数直接相关。
How do I improve the DataGridperformance?
如何提高DataGrid性能?
采纳答案by Alan
There are a few options you can turn on to help you on your DataGridobject
您可以打开几个选项来帮助您处理DataGrid对象
EnableColumnVirtualization = true
EnableRowVirtualization = true
These two are the main ones I think might help. Next try making your binding async
这两个是我认为可能会有所帮助的主要内容。接下来尝试使您的绑定异步
ItemsSource="{Binding MyStuff, IsAsync=True}"
And lastly, I've heard that setting a maximum height and width can help even if it above the max screen size, but I didn't notice a difference myself (claim had to do with auto size measuring)
最后,我听说设置最大高度和宽度即使高于最大屏幕尺寸也有帮助,但我自己没有注意到差异(声称与自动尺寸测量有关)
MaxWidth="2560"
MaxHeight="1600"
Also never put a DataGridin a ScrollViewer, because you will essentially lose virtualization. Let me know if this helps!
也永远不要在 aDataGrid中放入 a ScrollViewer,因为您将从本质上失去虚拟化。让我知道这是否有帮助!
回答by Constanta
Maybe try this instead of loading all 50 rows at once
也许试试这个而不是一次加载所有 50 行
http://www.codeproject.com/Articles/34405/WPF-Data-Virtualization
http://www.codeproject.com/Articles/34405/WPF-Data-Virtualization
回答by Rajat Ghalni
Check if you have property ScrollViewer.CanContentScrollset False.
Setting this property to false disables the virtualization in a way will degrade the performance of your Data-grid. For more clarification refer this CanContentScroll
检查您是否ScrollViewer.CanContentScroll设置了属性False。将此属性设置为 false 会以某种方式禁用虚拟化,这会降低数据网格的性能。有关更多说明,请参阅此CanContentScroll

