INotifyPropertyChange ~ PropertyChanged not firing when property is a collection and a new item is a

Posted by eponymous23 on Stack Overflow See other posts from Stack Overflow or by eponymous23
Published on 2010-06-07T17:38:11Z Indexed on 2010/06/07 17:42 UTC
Read the original article Hit count: 288

Filed under:
|

I have a class that implements the INotifyPropertyChanged interface. Some of the properties of the class are of type List. For example:

public List<string> Answers
{
    get { return _answers;  }
    set
    {
      _answers = value;
      onPropertyChanged("Answers")
    }
}

...

private void onPropertyChanged(string propertyName)
{
    if(this.PropertyChanged != null)
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

If I assign a new List<string> to Answer, then the PropertyChanged event fires as expected; but if I add a string string to the Answer list using the List Add method, then PropertyChanged event doesn't fire.

I was considering adding an AddAnswer() method to my class, which would handle calling the lists's Add method and would call onPropertyChanged() from there, but is that the right way to do it? Is there a more elegant way of doing it?

Cheers, KT

© Stack Overflow or respective owner

Related posts about c#

Related posts about inotifypropertychanged