添加到ObservableCollection和ListViewCollection分组

时间:2014-04-14 08:41:07

标签: wpf collections observablecollection

我有一个ObservableCollection,我使用ListViewCollection来提供特定属性的分组,在本例中为MyProperty。

问题是当我向ObservableCollection添加对象时,UI中的ListView中的分组没有更新,直到我在ListCollectionView上调用.Refresh()方法。我确实在ListView控件中看到了数据,但它没有分组。

我在Stuff对象中实现了INotifyPropertyChanged。以下是代码示例。

ObservableCollection<Stuff> MyStuff = new ObservableCollection<Stuff>();
MyListView.ItemSource = MyStuff;

MyListCollectionView =    (ListCollectionView)CollectionViewSource.GetDefaultView(MyListView.ItemsSource);
var groupDescription = new PropertyGroupDescription("MyProperty");
MyListCollectionView .GroupDescriptions.Add(groupDescription);

Stuff newStuff = new Stuff();
newStuff.MyProperty = "Group A";
MyStuff.Add(newStuff); // Once added here, ListView shows item, but doesn't show correct grouping at this point

任何人都会看到我可能做错了什么。提前感谢任何指示。

0 个答案:

没有答案