私は自分のコレクションをバインドするためのWPF ListViewを持っています。このコレクション内のオブジェクトのプロパティは、バックグラウンドスレッドで変更されます。プロパティが変更されたときにListViewを更新する必要があります。一部のオブジェクトのプロパティを変更すると、SourceUpdatedイベントが発生しません。WPF ListViewのソースを更新するには?
P.S. ItemSourceをnullに設定し、再バインドすると適切ではありません。
私は自分のコレクションをバインドするためのWPF ListViewを持っています。このコレクション内のオブジェクトのプロパティは、バックグラウンドスレッドで変更されます。プロパティが変更されたときにListViewを更新する必要があります。一部のオブジェクトのプロパティを変更すると、SourceUpdatedイベントが発生しません。WPF ListViewのソースを更新するには?
P.S. ItemSourceをnullに設定し、再バインドすると適切ではありません。
変化であったあなたのオブジェクトがINotifyPropertyChanged
を実装し、セッターは、あなたの財産で呼び出されたときに必要な変更通知を発生させていることを確認します。
// This is a simple customer class that
// implements the IPropertyChange interface.
public class DemoCustomer : INotifyPropertyChanged
{
// These fields hold the values for the public properties.
private Guid idValue = Guid.NewGuid();
private string customerNameValue = String.Empty;
private string phoneNumberValue = String.Empty;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
// The constructor is private to enforce the factory pattern.
private DemoCustomer()
{
customerNameValue = "Customer";
phoneNumberValue = "(555)555-5555";
}
// This is the public factory method.
public static DemoCustomer CreateNewCustomer()
{
return new DemoCustomer();
}
// This property represents an ID, suitable
// for use as a primary key in a database.
public Guid ID
{
get
{
return this.idValue;
}
}
public string CustomerName
{
get
{
return this.customerNameValue;
}
set
{
if (value != this.customerNameValue)
{
this.customerNameValue = value;
NotifyPropertyChanged("CustomerName");
}
}
}
public string PhoneNumber
{
get
{
return this.phoneNumberValue;
}
set
{
if (value != this.phoneNumberValue)
{
this.phoneNumberValue = value;
NotifyPropertyChanged("PhoneNumber");
}
}
}
}
あなたの代わりに、あなたはあなたのコレクションはObservableCollection<T>
自動である必要があります。オブジェクトのコンテナとしてObservableCollectionを使用するだけで、オブジェクトのクラスはINotifyPropertyChangedを実装する必要があります(リストビューに通知するプロパティのパターンを実装できます
)