【问题标题】:How to update source of WPF ListView?如何更新 WPF ListView 的源代码?
【发布时间】:2010-11-22 16:20:20
【问题描述】:

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

附:将 ItemSource 设置为 null 并重新绑定是不合适的。

【问题讨论】:

    标签: wpf listview itemsource


    【解决方案1】:

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

    MSDN

    【讨论】:

      【解决方案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>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        • 2011-06-24
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        相关资源
        最近更新 更多