gpt4 book ai didi

windows-8 - 在 WinRT 中,实现 INotifyCollectionChanged 而不是更新 ItemsControl 的集合

转载 作者:行者123 更新时间:2023-12-01 03:34:25 25 4
gpt4 key购买 nike

我正在编写自己的集合类,它也实现了 INotifyCollectionChanged。我在 Windows 8 商店应用程序 (winRT) 中使用它。我编写了一个单元测试,证明修改列表的内容会引发所有适当的事件,这些事件与“正常”可观察集合会引发的事件相同。不过,当我将 ItemsControl 的 ItemsSource 属性(我尝试了 GridView、ListView,甚至是普通的 ItemsControl)绑定(bind)到集合时,它不会在集合更改时影响 UI。

底层集合类型是否必须是 ObservableCollection 才能工作,或者是否可以编写我自己的集合类?

感谢

最佳答案

您可以使用 ICollectionView它还具有一些用于过滤的扩展功能。如果您想要预制类(class),请查看 one available at Code Project .

特别是,我注意到 UI 订阅了 VectorChanged事件,因此您应该只执行评论前面提到的 IObservableCollection

VectorChanged 事件采用类型为 IVectorChangedEventArgs 的接口(interface),我环顾四周时没有发现具体的类。不过创建一个并不难。下面是一个可以类似于创建 NotifyPropertyChangedEventArgs 实例的方法来创建的。它是私有(private)的,因为它只在集合类中使用。

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

关于windows-8 - 在 WinRT 中,实现 INotifyCollectionChanged 而不是更新 ItemsControl 的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14587966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com