刷新可观察的字典,wpf中的列表框
我在 WPF 应用程序中的列表框控件上绑定通用可观察字典。我每 5 秒获取一次新的数据作为可观察的字典。
我想在 wpf 应用程序中刷新这个新的字典列表框。
我的解决方案是:
//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);
}
我的解决方案效果很好,但是存在任何简单而好的方法吗?感谢您的想法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实施 INotifyPropertyChanged (它使任何内容“可观察”) WPF 知道您何时直接分配给好友列表。换句话说,让您的朋友列出属性而不是字段,并在
set
块上触发 PropertyChanged 委托:)希望有所帮助,请阅读链接,您会在那里找到更多信息。如果您有任何疑问,请评论。
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.
由于您有自己的
MyObservableDictionary
,因此您可以为其实现AddRange
函数,并像这样调用它:_friends.AddRange(_freshFriends);
如果您还想添加其他集合,这可以减少代码重复。Since you have your own
MyObservableDictionary
, you could implement anAddRange
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.