Windows 8 在WinRT中,实现INotifyCollectionChanged而不更新ItemsControl的集合
我正在编写自己的集合类,它也实现了INotifyCollectionChanged。我正在Windows8商店应用程序(winRT)中使用它。我编写了一个单元测试,它证明修改列表的内容会引发所有适当的事件,这些事件与“正常”可观察集合会引发的事件相同。但是,当我将ItemsControl的ItemsSource属性(我尝试了GridView、ListView,甚至是普通的ItemsControl)绑定到集合时,它不会影响集合更改时的UI 基础集合类型必须是可观察集合才能工作,还是可以编写自己的集合类 Thnx您可以使用具有一些扩展过滤功能的。如果你想要一个预先制作的类,请查看 特别是,我注意到UI订阅了事件,因此您应该只擅长实现前面注释中提到的Windows 8 在WinRT中,实现INotifyCollectionChanged而不更新ItemsControl的集合,windows-8,itemssource,inotifycollectionchanged,Windows 8,Itemssource,Inotifycollectionchanged,我正在编写自己的集合类,它也实现了INotifyCollectionChanged。我正在Windows8商店应用程序(winRT)中使用它。我编写了一个单元测试,它证明修改列表的内容会引发所有适当的事件,这些事件与“正常”可观察集合会引发的事件相同。但是,当我将ItemsControl的ItemsSource属性(我尝试了GridView、ListView,甚至是普通的ItemsControl)绑定到集合时,它不会影响集合更改时的UI 基础集合类型必须是可观察集合才能工作,还是可以编写自己的集
IObservableCollection
VectorChanged
事件采用类型为IVectorChangedEventArgs
的接口,我四处查看时没有发现具体的类。不过,创建一个并不困难。这里有一个类似于创建NotifyPropertyChangedEventArgs实例的方法。它是私有的,因为它只在collection类中使用
private sealed class VectorChangedEventArgs : IVectorChangedEventArgs
{
public VectorChangedEventArgs(NotifyCollectionChangedAction action, object item, int index)
{
switch (action)
{
case NotifyCollectionChangedAction.Add:
CollectionChange = CollectionChange.ItemInserted;
break;
case NotifyCollectionChangedAction.Remove:
CollectionChange = CollectionChange.ItemRemoved;
break;
case NotifyCollectionChangedAction.Move:
case NotifyCollectionChangedAction.Replace:
CollectionChange = CollectionChange.ItemChanged;
break;
case NotifyCollectionChangedAction.Reset:
CollectionChange = CollectionChange.Reset;
break;
default:
throw new ArgumentOutOfRangeException("action");
}
Index = (uint)index;
Item = item;
}
/// <summary>
/// Gets the affected item.
/// </summary>
public object Item { get; private set; }
/// <summary>
/// Gets the type of change that occurred in the vector.
/// </summary>
public CollectionChange CollectionChange { get; private set; }
/// <summary>
/// Gets the position where the change occurred in the vector.
/// </summary>
public uint Index { get; private set; }
}
私有密封类VectorChangedEventArgs:IVectorChangedEventArgs
{
公共向量changedEventArgs(NotifyCollectionChangedAction操作、对象项、int索引)
{
开关(动作)
{
案例NotifyCollectionChangedAction。添加:
CollectionChange=CollectionChange.ItemInserted;
打破
案例NotifyCollectionChangedAction。删除:
CollectionChange=CollectionChange.ItemRemoved;
打破
案例通知收集更改操作。移动:
案例通知收集更改操作。替换:
CollectionChange=CollectionChange.ItemChanged;
打破
案例通知CollectionChangedAction.Reset:
CollectionChange=CollectionChange.Reset;
打破
违约:
抛出新ArgumentOutOfRangeException(“操作”);
}
指数=(uint)指数;
项目=项目;
}
///
///获取受影响的项。
///
公共对象项{get;private set;}
///
///获取向量中发生的更改的类型。
///
公共集合更改集合更改{get;private set;}
///
///获取向量中发生更改的位置。
///
公共uint索引{get;private set;}
}
我注意到IObservableVector接口,但似乎ObservableCollection没有实现它(或者我错了)。它现在是唯一需要实现的“可观察的收集接口”了吗?也许,ObservableCollection
会自动投影到IObservableVector
。。。