I use DataContract with ObservableCollection:
[DataContract(Namespace = Terms.MyNamespace)]
public class MyContract
{
internal MyContract ()
{
List = new ObservableCollection<string>();
}
[DataMember]
private ObservableCollection<string> list;
[XmlArray("list")]
public ObservableCollection<string> List
{
get
{
return list;
}
set
{
list = value;
list.CollectionChanged += (s, e) =>
{
Console.WriteLine("It is never happens!! Why?");
};
}
}
...
So, when I work with my collection like this.
MyContract contract = new MyContract();
contract.List.Add("some");
Item was been added but CollectionChanged event not fired.
Why?
That is because you don’t serialize
Listwhile serializelist. So, during deserialization it won’t call setter of list, therefore won’t subscribe to event. In your case you can simply markListwithDataMemberAttributeinstead oflist, e.g.:Usage:
In this case event will fire.