使用通知ICollectionView更新BindableCollection中的项目 [英] Update item in BindableCollection with notify ICollectionView

查看:150
本文介绍了使用通知ICollectionView更新BindableCollection中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在视图上的ListBox控件上绑定了Caliburn Micro的集合.就是这个.

Hi I bind collection from Caliburn Micro on ListBox control in view. Here is it.

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

ListBox项是UserInfo的类型.

ListBox items is type of UserInfo.

我对列表框项目进行了排序和分组,我为此目的使用了CollectioView.

Hi I sort and group listbox items, I use CollectioView on this purpose.

当我初始化ListBox时,我使用这种方法对项目进行排序和分组.

When I initialize ListBox I sort and group items with this method.

private ICollectionView _currentView;

//...

private void SortContactList()
{
    _currentView = CollectionViewSource.GetDefaultView(Friends);

    _currentView.GroupDescriptions.Add(new PropertyGroupDescription("TextStatus"));

    _currentView.SortDescriptions.Add(new SortDescription("TextStatus", ListSortDirection.Ascending));

    _currentView.SortDescriptions.Add(new SortDescription("Nick", ListSortDirection.Ascending));
}

TextStatus和Nick是userInfo类的属性.

TextStatus and Nick are properties of userInfo class.

当我更新可绑定集合Friend中item的值时,我希望有一种方法可以通知集合视图此更改.因为我需要将项目移至正确/正确的组.

When I update values of item in bindable collection Friend I would like have a way how notify collection view about this change. Because I need move item to right/good group.

例如

Friend[0].TextStatus = "Ofline" -> is in offline group

我在线上更改价值;

Friend[0].TextStatus="Online" -> move in online group

在这里,我想通知好友视图(_currentView)关于好友集合的更改.

and here I want notify collection view (_currentView) about change on Friends collection.

推荐答案

在创建具有带有评分"列的表的应用程序时,我遇到了同样的问题. 我想知道为什么更改等级时行不会向上移动,最后我使用了Refresh方法.

I had the same issue when I created an application that had a table with the Rating column. I wondered why row doesn't move up when I change rating, and in the end I used the Refresh method.

以您的示例为例:

Friend[0].TextStatus="Online" -> move in online group
_currentView.Refresh();

幸运的是,没有发生性能问题,所以现在我在类似的情况下使用此解决方案.

Fortunately, performance problems didn't occur, so now I use this solution in similar situations.

这篇关于使用通知ICollectionView更新BindableCollection中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆