Add上的ObservableCollection + RaiseEvent

时间:2009-07-21 06:55:31

标签: c#

全部,

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

1 个答案:

答案 0 :(得分:9)

在添加,删除,更改,移动项目或刷新整个列表时会触发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; }
          }
      }