I have some JSON api on server and I want to use it for chat. So I use HttpWebRequest, Respond, … and Deserialize to ObservableCollection<ChatMessage>. It is everything ok. I have my class which is added to datacontext and that class contains collection above (ObservableCollection<ChatMessage> entries and it´s bind to listbox and everything is shown fine. Now if I want after some time reload content of that file on server I made everything that I made first time and replace collection in entries to new one. But this won´t fire PropertyChanged or CollectionChanged and view is still bound to old collection. So what´s the best “fix” of this?
I have some JSON api on server and I want to use it for
Share
Technically, your collection doesn’t change – it gets replaced by a new one so CollectionChanged won’t get fired.
You will need to implement INotifyPropertyChanged on your wrapping class, and when you replace the collection, fire the PropertyChanged event to notify the UI.
Another solution would be to not replace the collection, but to clear its items and add the new entries to that collection. You can do this in a smarter way by comparing the collections (old and new) and adding new entries / removing those that are no longer in your new list.