Update item in BindableCollection with notify ICol

2019-08-02 15:02发布

问题:

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 items is type of UserInfo.

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

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 and Nick are properties of userInfo class.

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.

for example

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

I change value on online;

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

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

回答1:

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.

For your example:

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.