Pete
Pete

Reputation:

ObservableCollection + RaiseEvent on Add

HI all,

How can I raise an event on when an item is added to an ObservableCollection?

Upvotes: 4

Views: 434

Answers (1)

chikak
chikak

Reputation: 1732

CollectionChanged gets fired when an item is added, removed, changed, moved, or the entire list is refreshed. You can subscribe to CollectionChanged to receive notifications.

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

Upvotes: 9

Related Questions