这是我遇到过的最奇怪的事情。在Windows 8中,MS从CollectionViewSource中删除了过滤和排序,但我不得不自己创建,名为CollectionView<T>
。 CollectionView
有一个类型为IObservableCollection<T>
的View属性,这是我为保持事物抽象而做出的自定义界面。它的定义很简单为什么没有ItemsSource绑定到我的自定义CollectionChanged事件
public interface IObservableCollection<T> : IReadOnlyList<T>, INotifyCollectionChanged
{
}
然后,我有实现这个接口我的内部类:
internal class FilteredSortedCollection<T> : IObservableCollection<T>
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
public void RaiseCollectionChanged(NotifyCollectionChangedEventArgs args)
{
var copy = CollectionChanged;
if (copy != null)
copy(this, args);
}
public Func<IEnumerator<T>> RequestEnumerator { get; set; }
public Func<int> RequestCount { get; set; }
public Func<int, T> RequestItem { get; set; }
public IEnumerator<T> GetEnumerator()
{
return RequestEnumerator();
}
public int Count { get { return RequestCount(); } }
public T this[int index] { get { return RequestItem(index); } }
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
事情一直工作到这里。 CollectionView正确过滤和订购,并且该视图按预期工作。除了当我将它绑定到一个ListView.ItemsSource属性它只是表现得好像它没有实现INotifyCollectionChanged
。没有人收听CollectionChanged事件(使用调试器进行检查)并且UI不会使用添加的新元素更新。但是,如果我添加一些项目,然后设置ItemsSource属性,UI更新。就好像它是一个正常的,不可观察的清单。
有人知道这里会发生什么吗?我尝试删除IObservableCollection
接口,因此FilteredSortedCollection
刚刚直接实施了IReadOnlyList<T>
和INotifyCollectionChanged
,但它没有奏效。
1)它与其他ItemControls(列表框)工作的? –
2)当用IList替换IReadOnlyList 时会发生什么情况? –
它不能与其他ItemControls一起使用,并且使用IList也无济于事。我也尝试用IEnumerable 而不是IReadOnlyList 而没有。 –
gjulianm