0

我有一个 WPF ListView 来绑定我的集合。此集合中对象的属性在后台线程中更改。更改属性时,我需要更新 ListView。当我更改某些对象的属性时,不会触发 SourceUpdated 事件。

PS 将 ItemSource 设置为 null 然后重新绑定是不合适的。

4

2 回答 2

3

这应该是自动的,您只需要使用 ObservableCollection 作为对象的容器,并且对象的类需要实现 INotifyPropertyChanged (您可以只为要通知列表视图发生更改的属性实现模式)

MSDN

于 2010-11-22T16:26:15.717 回答
2

确保您的对象INotifyPropertyChanged在您的属性上调用 setter 时实现并引发所需的更改通知。

// 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>

于 2010-11-22T16:35:46.197 回答