刷新列表框中的数据 - 停止移动滚动条并闪烁所选项目

发布于 2024-10-16 05:55:37 字数 1495 浏览 9 评论 0原文

我尝试解决这个问题。在 WFP 应用程序中,我将 binadble 集合绑定到 listBox 的属性 ItemSource。

属性签名为:

    public BindableCollection<UserInfo> Friends
    {
        get { return _friends; }
        set
        {
            _friends = value;
            NotifyOfPropertyChange(() => Friends);
        }
    }

U​​serInfo 类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘你却要生生世世 2024-10-23 05:55:37

我预计问题是您的 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 set SelectedFriend to the item whose Nick matches the stored value.

寂寞陪衬 2024-10-23 05:55:37

为了防止列表框滚动条跳跃,请勿从 BindableCollection 中删除然后添加回所有项目。

将 RefreshContactsData 重写为:

  1. 删除 Friends 中存在但 freshFriends 中不再存在的项目。
  2. 添加 freshFriends 中存在但 Friends 中不存在的项目。
  3. 更新 Friends 和 freshFriends 中的现有项目。

您可能需要在 UserInfo 上实现 INotifyPropertyChanged。

To prevent listbox scrollbar jumping do not remove then add back all items from the BindableCollection.

Rewrite RefreshContactsData to:

  1. Delete items that exist in Friends that no longer exist in freshFriends.
  2. Add items that exist in freshFriends that do not exist in Friends.
  3. Update existing items in Friends and freshFriends.

You may need to implement INotifyPropertyChanged on UserInfo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文