降低绑定DataGrid的刷新率

时间:2010-12-15 10:27:57

标签: c# wpf data-binding datagrid refresh

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

e.g。

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

3 个答案:

答案 0 :(得分:2)

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

假设您的数据类型为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<string> changedProperties = new List<string>();

    // 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&lt; LazyRecord&gt;来自你的名单&lt;记录&gt;并将其用作数据源。显然,使用通用解决方案是直截了当的,这种解决方案更加可重用。希望我帮了一下。

答案 1 :(得分:1)

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

  • 将数据的卷影副本绑定到gui元素,而不是绑定原始数据。
  • 添加一个eventhandler,用于更新与原始数据相比有一定延迟的卷影副本。

您可以在类似的新问题how-to-do-the-processing-and-keep-gui-refreshed-using-databinding

上找到更多更好的答案

答案 2 :(得分:-1)

尝试listView.Items.Refresh();

相关问题