已变更项目的集合观察的集合属性 [英] Observable Collection Property Changed on Item in the Collection

查看:164
本文介绍了已变更项目的集合观察的集合属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ObservableCollection< T> 。我已经将它绑定到一个ListBox控件和我添加 SortDescriptions 来Items集合的列表框,使列表排序我多么希望。

I have an ObservableCollection<T>. I've bound it to a ListBox control and I've added SortDescriptions to the Items collection on the ListBox to make the list sort how I want.

我想在任何财产的子元素上更改诉诸的列表中的任何点。

I want to resort the list at ANY point when any property changed on a child element.

我所有的子元素贯彻 INotifyPropertyChanged的

推荐答案

蛮力:


  1. 附加处理程序,以每个PropertyChanged事件的每个子项

  2. 从您的CollectionViewSource

  3. 呼叫刷新抢的ListCollectionView。

< STRONG>编辑:

中的代码为1,2就住在你的后台代码

The code for 1, 2 would live in your code-behind.

有关#1,你会做这样的事情:

For #1, you'd do something like:

private void Source_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    switch (e.Action)
    {
        case NotifyCollectionChangedAction.Add:
            foreach( SomeItem item in e.NewItems)
            {
               item.PropertyChanged += new PropertyChangedEventHandler(_SomeItem_PropertyChanged); 
            }
            break;
....
**HANDLE OTHER CASES HERE**
....
      }
}

有关#2,在你CollectionChanged处理程序中,你会做这样的事情:

For #2, in your CollectionChanged handler, you would do something like:

private void _SomeItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    ListCollectionView lcv = (ListCollectionView)(CollectionViewSource.GetDefaultView(theListBox.ItemsSource));
    lcv.Refresh();
}



EDIT2:
然而,在这种情况下,我的强烈建议你也检查ListCollectionView.NeedsRefresh且仅当它被设置刷新。没有理由,如果你的属性已更改不影响排序进行重新排序。

However, in this case, I would strongly suggest that you also check ListCollectionView.NeedsRefresh and only refresh if that is set. There's no reason to re-sort if your properties have changed which don't affect the sort.

这篇关于已变更项目的集合观察的集合属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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