에 더 좋은 답을 찾을 수 있음.
데이터가 List < Record> 유형이고 DataGrid에 바인딩되어 있다고 가정 해 보겠습니다.
데이터 (이 경우 한 행)에 대한 래퍼 클래스가 필요합니다. 이 래퍼는 속성을 변경하고 규칙적으로 실행합니다. 주의 사항 : 테스트를 거치지 않고이 코드를 작성 했으므로 버그가있을 수 있습니다. 또한 스레드로부터 안전하지 않으므로 목록을 사용하여 작업 할 때 잠금을 추가해야합니다. 그러나 요점은 타격을 받아야한다.
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));
}
}
}
이 후, 당신의 목록 < 기록에서 목록 < LazyRecord>을 만들>와 데이터 소스로 것을 사용합니다. 분명히 훨씬 더 재사용 가능한 일반적인 솔루션을 사용하는 것이 간단합니다. 희망을 조금 도왔습니다.
이 질문은 WPF로 태그되었으므로 DataGrid를 사용한다고 생각하십니까? DataGridView는 Windows Forms입니다. 또한 열 또는 컬렉션이 빠르게 변경됩니까? –
@Meleak - 네, 미안 해요 WPF에서 DataGrid 의미. 컬렉션이 빠르게 변경되므로 DataGrid가 INotifyPropertyChanged 이벤트에서 너무 자주 업데이트됩니다. DataGrid에서 속성 변경 이벤트 중 일부를 기본적으로 무시하고, 한 번만 업데이트하면됩니다. – Chad
사용자가 DataGrid의 데이터를 업데이트 할 수 없습니까? 그렇지 않은 경우 동기화 문제를 확인하려면 지연된 업데이트로 해결해야합니다. – whoisthis