ObservableCollection does not change property changed by Fody
I have defined this class
[ImplementPropertyChanged]
public class ChatInfo : IMessageData
{
public ObservableCollection<Message> messages { get; set; }
[JsonIgnore]
public Message LastMessage
{
get
{
return messages.Last();
}
}
}
The class is deserialized via JSON.NET (James King) and then after I manually push the new elements to messages
. The property is LastMessage
bound to the view, but even if the collection changes messages
, the LastMessage
getter is not called. When I install messages
to a new collection every time everything works fine, why LastMessage
doesn't it respond to collecting changed events from ObservableCollection
?
source to share
LastMessage does not respond to collection changes because the message remains unchanged. You need to subscribe to your observable collection event.
[ImplementPropertyChanged]
public class ChatInfo : IMessageData
{
public ObservableCollection<Message> messages { get; set; }
messages.CollectionChanged += new
System.Collections.Specialized.NotifyCollectionChangedEventHandler(MessageChanged);
[JsonIgnore]
public Message LastMessage {get; private set;}
private void MessageChanged(object sender, NotifyCollectionChangedEventArgs e)
{
//set the property here
LastMessage = messages.Last();
}
}
source to share