【发布时间】:2011-06-22 05:43:52
【问题描述】:
我尝试解决这个问题。在 WFP 应用程序中,我将 binadble 集合绑定到 listBox 的属性 ItemSource。
属性签名是:
public BindableCollection<UserInfo> Friends
{
get { return _friends; }
set
{
_friends = value;
NotifyOfPropertyChange(() => Friends);
}
}
UserInfo 类构成属性:
- BitmapImage ProfilePhoto {get;set;}
- 字符串尼克{get;set}
- String Status{get;set;} //离线、在线、聊天
- String ChatRoom{get;set;} //用户聊天的聊天室名称
我每 10 秒获取一次新数据作为 IDictionary => ()。
我需要刷新列表框中的数据。所以我试试这个:
private void RefreshContactsData(IEnumerable<KeyValuePair<string, UserInfo>> freshFriends)
{
//store selected item in listBox
var tempSelectedFriend = SelectedFriend;
//store selecte index in listbox
var tempSelectedIndex = SelectedFriendsIndex;
//Clear property which is binded on listBox ItemSource
Friends.Clear();
foreach (var freshFriend in freshFriends)
{
freshFriend.Value.IsFriend = true;
//Add fresh data
Friends.Add(freshFriend.Value);
}
StayInRoom();
//set
SelectedFriend = tempSelectedFriend;
SelectedFriendsIndex = tempSelectedIndex;
}
问题是:
我将当前选定的项目存储在列表框中,清除列表框,添加新数据,并将选定的项目设置回列表框中。 但是用户看到滚动条被移动并向后移动,并且选定的项目也闪烁了。
如何消除这种不受欢迎的行为。
【问题讨论】:
标签: c# wpf listbox refresh scrollbar