0

请在下面查看我的代码

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;
        }

    }

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

谢谢

4

1 回答 1

0

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

     TestList.CollectionChanged += TestList_CollectionChanged;

     void TestList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
            {
                if (e.NewItems != null)
                {
                    /// Filter the PersonListView
                }
            }
于 2015-12-30T11:24:18.063 回答