【问题标题】:How to refresh ICollectionView when new item added into it's internal ObservableCollection将新项目添加到其内部 ObservableCollection 时如何刷新 ICollectionView
【发布时间】:2015-12-30 09:41:13
【问题描述】:

请看下面我的代码

private ObservableCollection<Person> testList = new ObservableCollection<Person>();

    public ObservableCollection<Person> TestList
    {
        get { return testList; }
        set
        {
            if (testList != value)
            {
                testList = value;
                SetProperty<ObservableCollection<Person>>(ref testList, value);
            }
        }
    }

    private ListCollectionView personListView;

    public ICollectionView PersonListView
    {
        get
        {
            if (personListView == null)
            {
                personListView = new ListCollectionView(TestList)
                {
                    Filter = p => FilterPerson((Person)p)
                };
            }
            return personListView;
        }

    }

我想知道如何更新 PersonListView 并在将新项目插入 TestList 或重新分配整个 TestList 时刷新 UI?我试过在TestList set方法中调用Refresh方法,但是当一个新项目插入到集合中时,set方法不会被调用。

谢谢

【问题讨论】:

标签: wpf datagridview observablecollection icollectionview


【解决方案1】:

我认为您可以将集合更改事件添加到 TestList 可观察集合并过滤其中的 PersonListView。

     TestList.CollectionChanged += TestList_CollectionChanged;

     void TestList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
            {
                if (e.NewItems != null)
                {
                    /// Filter the PersonListView
                }
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    相关资源
    最近更新 更多