当新项目添加到它的内部 ObservableCollection 时如何刷新 ICollectionView

How to refresh ICollectionView when new item added into it's internal ObservableCollection

请看下面我的代码

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方法不会被调用。

谢谢

我认为你可以向 TestList 可观察集合添加一个集合更改事件,并在其中过滤 PersonListView。

     TestList.CollectionChanged += TestList_CollectionChanged;

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