Refresh observable dictionary , listbox in wpf
I bind generic observable dictionary on listbox control in WPF app. I get every 5 sec new fresh data as observable dictionary.
I would like refresh with this new dictionary listbox in wpf app.
My soulution is :
//this dic is bind on listbox
private MyObservableDictionary<string, Friend> _friends;
//new data
private MyObservableDictionary<string, Friend> _freshFriends开发者_运维问答;
....
//get data from server
_freshFriends = _service.LoadFriends(Account);
_friends.Clear();
//refresh dic
foreach (var freshFriend in _freshFriends)
{
_friends.Add(freshFriend);
}
My soulution works good, but exist any simple and nice way ? Thank for ideas.
Implement INotifyPropertyChanged (It makes anything "observable") so WPF knows when you directly assign to the friends list. In other words, make your friends list a property instead of a field and fire the PropertyChanged delegate on the set
block :)
Hope that helps, read the link you will find more info there. Comment if you have any questions.
Since you have your own MyObservableDictionary
, you could implement an AddRange
function to it and just call it like this: _friends.AddRange(_freshFriends);
This could reduce code duplication if you wanted to add other collections as well.
精彩评论