ObservableCollection + RaiseEvent on Add

4

大家好,

当ObservableCollection中添加了一个项时,我该如何触发事件?

1个回答

9

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

来自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; }
          }
      }

1
秒数在里面... Jon 和我都有“不够快”的已删除答案 ;-p - Marc Gravell
比Marc和Jon回答更快——这至少应该获得银牌! - Treb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接