大家好,

如何在将项目添加到 ObservableCollection 时引发事件?

最佳答案

当添加、删除、更改、移动项目或刷新整个列表时,会触发 CollectionChanged。您可以订阅 CollectionChanged 以接收通知。

From MSDN

 public class NameList : ObservableCollection<PersonName>
    {
        public NameList() : base()
        {
            Add(new PersonName("Willa", "Cather"));
            Add(new PersonName("Isak", "Dinesen"));
            Add(new PersonName("Victor", "Hugo"));
            Add(new PersonName("Jules", "Verne"));
        }
      }

      public class PersonName
      {
          private string firstName;
          private string lastName;

          public PersonName(string first, string last)
          {
              this.firstName = first;
              this.lastName = last;
          }

          public string FirstName
          {
              get { return firstName; }
              set { firstName = value; }
          }

          public string LastName
          {
              get { return lastName; }
              set { lastName = value; }
          }
      }

关于c# - 添加时 ObservableCollection + RaiseEvent,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1157632/

10-09 01:39