c# - 获取 ICollectionView 的 Count 属性

标签 c# wpf mvvm collections observablecollection

我有 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 方法的定义。

最佳答案

这样试试;

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

ICollectionView工具 IEnumarable但它没有实现 IEnumerable<TSource>喜欢List<TSource> , HashSet<TSource>所以,ICollectionView没有通用类型,我们必须将其转换为 IEnumerable<object>一次启用 Count()方法。

关于c# - 获取 ICollectionView 的 Count 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47854184/

相关文章:

wpf - UI 未调用 INotifyDataErrorInfo.GetErrors()

c# - 将用户控件打印到 A4 页面的中心

wpf - 为什么在定义TreeView.ItemTemplate后TreeView不会自动为子节点选择DataTemplates?

wpf - 在服务上实现 INotifyCollectionChanged 或 INotifyPropertyChanged 可以吗?

WPF DataContext不使用MVVM模型刷新DataGrid

c# - 64 位进程中的 AnyCPU C# DLL 加载 32 位 DLL

.net - WPF Datagrid 模板删除了调整列大小的能力

c# - 如何使用 .NET 压缩目录?

c# - 从 EntityFramework 4 中的多对多关系中删除

c# - PictureBox 在 RotateFlipType 之后不重绘,但它使用断点