【发布时间】:2016-06-06 03:14:57
【问题描述】:
我创建了自己的集合并实现了 INotifyCollectionChanged。
public class ObservableSortedSet<T> : SortedSet<T>, INotifyCollectionChanged
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
public new bool Add(T item)
{
var result = base.Add(item);
if (result)
CollectionChanged?.Invoke(item,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
return result;
}
public new bool Remove(T item)
{
var result = base.Remove(item);
if (result)
CollectionChanged?.Invoke(item,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
return result;
}
public new void Clear()
{
base.Clear();
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
但是,当我尝试将此集合用作视图中的 ItemsSource 时,它们不会自动更新,例如删除一个项目。正如我在这里的其他问题中看到的那样,我应该实现 INotifyCollectionChanged。我这样做了,但它不起作用。有什么建议吗?
【问题讨论】: