降低绑定DataGrid的刷新率

我的WPF / C#应用程序中有一个绑定到Entity Framework集合的DataGrid。 每行都有非常频繁更改的绑定列 – 每秒多次。 这导致列基本上不可读,因为它经常变化。 如何强制WPF每隔0.5秒或1秒仅显示一个新值,即使该值每隔0.1秒更改一次?

例如

dataGrid.MaxRefreshRate = 1000; (value in milliseconds). 

我认为您需要在数据和数据网格之间创建一个图层。

假设您的数据类型为List ,并且此时它与DataGrid绑定。

我们需要一些数据包装类(在这种情况下为一行)。 这个包装器会更改属性并定期触发它。 注意:我在没有任何测试的情况下用心编写了这段代码,可能(并且会)是bug。 它也不是线程安全的,在使用列表时需要添加一些锁。 但重点应该是打击。

 public class LazyRecord : INotifyPropertyChanged { private string name; public string Name { get { return name; } set { if (name != value) { name = value; OnPropertyChanged("Name"); } } // other properties // now the important stuff - deffering the update public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (this.changedProperties.Find(propertyName) == null) this.changedProperties.Add(propertyName); } private readonly List changedProperties = new List(); // and the timer that refreshes the data private readonly Timer timer; private readonly Record record; public LazyRecord(Record record) { this.timer = new Timer(1000, OnTimer); this.record = record; this.record.OnPropertyChanged += (o, a) => this.OnPropertyChanged(a.PropertyName); } public void OnTimer(..some unused args...) { if (this.PropertyChanged != null) { foreach(string propNAme in changedProperties) { PropertyChanged(new PropertyChangedEventArgs(propName)); } } } 

在此之后,只需从List 创建List 并将其用作数据源。 显然,使用通用解决方案是直截了当的,这种解决方案更加可重用。 希望我帮了一下。

只是一个想法它是如何工作的。

  • 将数据的卷影副本绑定到gui元素,而不是绑定原始数据。
  • 添加一个事件处理程序,用于从原始数据中以一定的延迟更新阴影副本。

也许你会在类似的新问题上找到更多更好的答案。 如何处理和保持gui -rereshed-using-databinding

试试listView.Items.Refresh();