我有一些实现INotifyCollectionChanged的集合。
public sealed class GroupCollection : INotifyCollectionChanged, IList<Group>
{
//...
public event NotifyCollectionChangedEventHandler CollectionChanged;
private void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (CollectionChanged != null)
{
CollectionChanged(this, e);
}
}
//...
}它在xaml中使用
<CollectionViewSource x:Name="groupedItemsViewSource" Source="{Binding Groups}"/>然后在xaml.cs中
this.DefaultViewModel["Groups"] = groups.GroupCollection;集合项目可以很好地显示。但UI不订阅CollectionChanged,并且在触发CollectionChanged时不会自动更新。也许我需要实现更多的接口来使UI控件订阅事件?
附注:我不能使用ObservableCollection,因为编译器说这“不是一个Windows Runtime接口”。
发布于 2013-03-06 23:49:57
在使用默认的网格应用程序模板时,ObservableCollection的以下用法适用于我:
using System.Collections.ObjectModel;
...
class MyOC : ObservableCollection<SampleDataGroup> { };
...
var oc = new MyOC();
string id = "title1";
oc.Add(new SampleDataGroup(id, id, id, "", id));
id = "title2";
oc.Add(new SampleDataGroup(id, id, id, "", id));
this.DefaultViewModel["Groups"] = oc;我猜你可以在你的项目中做一些类似的事情:
using System.Collections.ObjectModel;
...
public sealed class GroupCollection : ObservableCollection<Group>
{
...https://stackoverflow.com/questions/15251347
复制相似问题