2015-12-30 41 views
0

請參考下面如何刷新ICollectionView當有新產品加入到它的內部的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並刷新UI我的代碼時,新項目插入TestList,或整個TestList被重新分配?我試過在TestList set方法中調用Refresh方法,但是當一個新項插入到集合中時,set方法將不會被調用。

感謝

+0

http://stackoverflow.com/questions/16297983/inotifycollectionchanged-is-not-updating-the-ui – A191919

回答

0

我認爲你可以收集改變的事件添加到TestList觀察到的集合,並在其中篩選PersonListView。

 TestList.CollectionChanged += TestList_CollectionChanged; 

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