刷新列表框中的数据 - 停止移动滚动条并闪烁所选项目
我尝试解决这个问题。在 WFP 应用程序中,我将 binadble 集合绑定到 listBox 的属性 ItemSource。
属性签名为:
public BindableCollection<UserInfo> Friends
{
get { return _friends; }
set
{
_friends = value;
NotifyOfPropertyChange(() => Friends);
}
}
UserInfo 类 consit property:
- BitmapImage ProfilePhoto {get;set;}
- String Nick{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;
}
问题是:
我将当前选定的项目存储在列表框中,清除列表框,添加新数据,然后在列表框中设置回选定的项目。 但是用户看到滚动条被移动并向后移动,并且所选项目也闪烁。
我怎样才能消除这种不需要的行为。
I try solve this problem. In WFP app I bind binadble collection to the property ItemSource of listBox.
Property signature is:
public BindableCollection<UserInfo> Friends
{
get { return _friends; }
set
{
_friends = value;
NotifyOfPropertyChange(() => Friends);
}
}
UserInfo class consit property:
- BitmapImage ProfilePhoto {get;set;}
- String Nick{get;set}
- String Status{get;set;} //offline, online, chatting
- String ChatRoom{get;set;} //name of chat room where user chatting
I get every 10 seconds new fresh data as IDictionary => ().
I need refresh data in listbox. So I try this:
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;
}
Problems is:
I store current current selected item in listBox, clear listbox, add new data, and set back selected item in listbox.
But user see that scrollbar is moved and moved back and also selected item flashed.
How can I remove this unwanted behavior.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我预计问题是您的
SelectedFriend
属性引用了一个引用,当您的集合重新填充时,该引用不再存在。 UserInfo 上是否有唯一标识用户的属性? (尼克
?)。您可以存储此选定的值,然后在重新填充集合后,查找SelectedFriend
并将其设置为Nick
与存储的值匹配的项目。I expect that the issue is your
SelectedFriend
property refers to a reference which no longer exists when your collection is repopulated. Is there any property on UserInfo which uniquely identifies a user? (Nick
?). You could store this selected value instead, and then after repopulating the collection, find and setSelectedFriend
to the item whoseNick
matches the stored value.为了防止列表框滚动条跳跃,请勿从 BindableCollection 中删除然后添加回所有项目。
将 RefreshContactsData 重写为:
您可能需要在 UserInfo 上实现 INotifyPropertyChanged。
To prevent listbox scrollbar jumping do not remove then add back all items from the BindableCollection.
Rewrite RefreshContactsData to:
You may need to implement INotifyPropertyChanged on UserInfo.