简体   繁体   English

慢速WPF 4数据网格刷新

[英]Slow WPF 4 Datagrid Refresh

I am using the WPF DataGrid component from the .NET 4 framework, it is bound to a thread safe observable collection which I found here : http://www.deanchalk.me.uk/post/Thread-Safe-Dispatcher-Safe-Observable-Collection-for-WPF.aspx 我正在使用.NET 4框架中的WPF DataGrid组件,它绑定到我在此处找到的线程安全可观察集合: http//www.deanchalk.me.uk/post/Thread-Safe-Dispatcher-Safe-观察到的收集换WPF.aspx

The program is a System Admin tool which pings each IP Address in a range, if there is a response it creates an object in the collection with some details from the computer. 该程序是一个系统管理工具,可以在一个范围内ping每个IP地址,如果有响应,它会在集合中创建一个具有计算机详细信息的对象。

The problem I am having, is poor performance. 我遇到的问题是性能不佳。 Initially, I was just updating the Collection and letting the datagrid pick up the changes. 最初,我只是更新Collection并让datagrid获取更改。 This caused an issue where the DataGrid control wasn't refreshing and showing the data unless I scrolled. 这导致了一个问题,即DataGrid控件没有刷新并显示数据,除非我滚动。

So I added a timer to call the grids refresh method, the timer ticks every 750ms. 所以我添加了一个计时器来调用网格刷新方法,计时器每750ms计时一次。 This worked brilliantly, until I realised that the programs UI is completely unresponsive while the timer is enabled and it is scanning. 这工作非常出色,直到我意识到程序UI在启用计时器并且正在扫描时完全没有响应。

Without the timer, performance is acceptable, without it, its terrible. 没有计时器,性能是可以接受的,没有它,它的可怕性。 I have tried several values for the timeout period (up to 2000ms) without any luck and have also made sure that my columns are fixed width (I read that autogenerated columns and widths can cause performance issues). 我已经尝试了几个值超时期间(最多2000毫秒),没有任何运气,并确保我的列是固定宽度(我读到自动生成的列和宽度可能会导致性能问题)。

The amount of Row's in the grid is about 300 - 400 with 5 columns, so its not exactly a huge grid. 网格中Row的数量大约为300 - 400,有5列,因此它不是一个巨大的网格。

Does anyone have any suggestions of how I could get the grid to update in a timely manner when the collection behind is added to, without sacrificing performance? 有没有人有任何关于如何在不牺牲性能的情况下及时更新网格时更新网格的建议?

If your bindings are correct you won't need to refresh the DataGrid manually. 如果绑定正确,则无需手动刷新DataGrid。 Updating the observable collection behind the scene should automatically trigger(assuming you have implemented NotifyPropertyChanged) the update of DataGrid. 更新场景后面的可观察集合应该自动触发(假设您已经实现了NotifyPropertyChanged)DataGrid的更新。 300 - 400 is not a number that will slow down the DataGrid. 300 - 400不是一个会减慢DataGrid速度的数字。 Is your DataGrid inside ScrollViewer? 你的DataGrid在ScrollViewer中吗? sometimes that degrades the performance as well. 有时也会降低性能。 Make sure your bindings are working fine then you won't need manual refresh. 确保您的绑定工作正常,然后您不需要手动刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM