Slow Down Refresh Rate Of Bound DataGrid

↘锁芯ラ 提交于 2019-12-14 01:47:20

问题


I've got a DataGrid in my WPF/C# app that is bound to an Entity Framework collection. Each row has bound columns that change very frequently - many times per second. This causes the column to basically be unreadable because it changes so often. How can I force WPF to only show a new value every .5 seconds or 1 second even if the value changes every .1 second?

e.g.

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

回答1:


I think you need to create a layer between your data and the datagrid.

Let's assume your data is of type List< Record > and at the moment it's bound to your DataGrid.

We'll need some wrapper class for your data (for one row in this case). This wrapper deffers property changed and fires it regularly. Notice: I wrote this code by heart without any testing, there might (and will) be bugs. It is also not thread-safe, you need to add some locks when working with the list. But the point should be hit.

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));
        }
    }
}

After this, just create a List< LazyRecord > from your List< Record > and use that as your datasource. Obviously it is straightforward to use a generic solution, which is much more reusable. Hope I helped a little.




回答2:


Just an Idea how it could work.

  • Have a shadow copy of your data bound to the gui element instead of binding the original data.
  • Add an eventhandler that update the shadow-copy with a certain delay from the original data.

May be you find more and better answers on a similar newer question how-to-do-the-processing-and-keep-gui-refreshed-using-databinding




回答3:


Try listView.Items.Refresh();.



来源:https://stackoverflow.com/questions/4448916/slow-down-refresh-rate-of-bound-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!