2012-08-28 29 views
1

这是我遇到过的最奇怪的事情。在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,但它没有奏效。

+0

1)它与其他ItemControls(列表框)工作的? –

+0

2)当用IList 替换IReadOnlyList 时会发生什么情况? –

+0

它不能与其他ItemControls一起使用,并且使用IList 也无济于事。我也尝试用IEnumerable 而不是IReadOnlyList 而没有。 – gjulianm

回答

1

您的收藏需要实施IList。我刚刚遇到了同样的问题,我实施了在Windows Phone应用程序中运行良好的IList,但是当我试图在Windows 8应用程序中使用视图模型时,它不遵守更改的事件。

我加的IList的实现上我的课,现在一切都按预期

相关问题