我有 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/