我的WPF/C#应用程序中有一个绑定到实体框架集合的DataGrid。每一行都有绑定的列,这些列更改非常频繁-每秒更改很多次。这会导致该列基本上不可读,因为它经常更改。如何强制WPF仅每.5秒或1秒显示一个新值,即使该值每.1秒更改一次?
例如:
dataGrid.MaxRefreshRate = 1000; (value in milliseconds).
发布于 2010-12-30 11:28:31
我认为你需要在你的数据和数据网格之间创建一个层。
让我们假设您的数据是List< Record >类型,此时它绑定到您的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<记录>创建一个List< LazyRecord >,并将其用作您的数据源。显然,使用通用解决方案是直接的,它更具可重用性。希望我能帮上点忙。
发布于 2010-12-26 03:07:38
只是一个关于它如何工作的想法。
也许你会在一个类似的新问题how-to-do-the-processing-and-keep-gui-refreshed-using-databinding上找到更多更好的答案
发布于 2010-12-15 19:24:10
试试listView.Items.Refresh();
。
https://stackoverflow.com/questions/4448916
复制相似问题