【问题标题】:Get Count property of ICollectionView获取 ICollectionView 的 Count 属性
【发布时间】:2018-05-30 22:52:26
【问题描述】:

我有 ICollectionView

private ICollectionView _snapshotList;

    public ICollectionView SnapshotList {
        get { return _snapshotList; }
    }

我在 ViewModel 构造函数中设置,其中this.smapshotListModel.SnapshotItems 返回 ObservableCollection

_snapshotList = CollectionViewSource.GetDefaultView(this.snapshotListModel.SnapshotItems);
        _snapshotList.Filter = ErrorMsgFilter;
        _snapshotList.CollectionChanged += OnCollectionChanged;

后来在 collectionChanged 事件中,我试图获取此集合中的项目数,

        void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
        this.ItemCount = _snapshotList.Count();
    }

但它的抛出错误,即没有定义 Count 方法。

【问题讨论】:

    标签: c# wpf mvvm collections observablecollection


    【解决方案1】:

    这样试试;

    _snapshotList.Cast<object>().Count();
    

    ICollectionView 实现了IEnumarable,但它没有像List&lt;TSource&gt;HashSet&lt;TSource&gt; 等那样实现IEnumerable&lt;TSource&gt;。所以,ICollectionView 没有泛型类型,我们必须将其转换为@987654328 @ 一次启用Count() 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 2015-01-29
      相关资源
      最近更新 更多