问题
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