views:

147

answers:

1

Greetings, I have some List of objects of Type CMessage. CMessage can look as follows:

public ROWGUID {get;set;}
public ObservableCollection<CAnswer> Answers 
{
get {return  _Answer;}
set 
{_Answer=value
RaisePropertyChanged("Answer");
}

}

each property has RaiseNotifyPropertyChanged method which Is implementation of INotifyPropertyChanged interface. The point is:

  1. I select some Message From the list of ObservableCollection Messages;
  2. I add some Answers to my selected message

Why There is no RaisePropertyChanged executed for that message? It should!

+2  A: 

You're doing this:

// get some Message from OC<Message> collection Message
var message = Messages.First(); 
message.Answers.Add(new CAnswer { Text = "HURRDURR" }); // add an answer

but your CMessage will only call RaisePropertyChanged when you do this

var message = Messages.First();
var answers = new ObservableCollection<CAnswer>();
answers.Add(new CAnswer { Text = "LOL" });
message.Answers = answers; // triggers here

The first raises the CollectionChanged event of the Answers collection. The second changes the Answers collection which will cause your set method to fire, which raises the PropertyChanged event of your CMessage class.

BTW, you're doing this wrong. You shouldn't let people set your collection property. It isn't a best practice, allows the property to be set to null (which is bad), etc. You should only have read-only property collections. If users are interested in the property changing they should subscribe to the CollectionChanged event of your property.

Will

related questions