Why is there no .Net interface for ICollectionView <T>?

Why is there no .Net interface for ICollectionView<T> ? Looking at the ICollectionView , it seems pretty obvious to expect an ICollectionView<T> .

Did I miss something?

+4
source share
1 answer

ICollectionView is only implemented by the CollectionView class. The MSDN documentation indicates that CollectionView should not be created in your code, but instead use the CollectionViewSource object to get your collection View.

If you want your collection T to return to CollectionView, you need to add your own collection (IEnumerable implementation) to the CollectionViewSource object and get a CollectionView there, for example:

  List<MyClass> listToView = new List<MyClass>(); MyClass x1 = new MyClass() { Name = "Fictive Name 1", Description = "Description...", Date = DateTime.Now}; MyClass x2 = new MyClass() { Name = "Fictive Name 2", Description = "Description...", Date = DateTime.Now}; MyClass x3 = new MyClass() { Name = "Fictive Name 3", Description = "Description...", Date = DateTime.Now}; listToView.Add(x1); listToView.Add(x2); listToView.Add(x3); CollectionViewSource collectionViewSource = new CollectionViewSource(); collectionViewSource.Source = listToView; ICollectionView collectionView = collectionViewSource.View; 

The reason why there is no ICollectionView for T is probably because it is not designed that way. The documentation indicates that CollectionView was designed to give a different view of the collection without modification:

You can present the collection view as a layer on top of the source binding, which allows you to navigate and display collections based on sorting, filtering and group queries, without having to manipulate the main source collection itself.

In this sense, it makes sense only to view the collection, hence the name "ViewCollection".

I think it is not so obvious to expect that ICollectionView for T as CollectionView will not be created at all for instantiation (see an interesting warning below, adding some sorting capabilities).

System.Windows.Data Warning: 52: Using CollectionView directly is not fully supported. Basic functions work, although with some inefficiencies, but advanced functions may encounter known errors. Consider using a derived class to avoid these problems.

I think that the architecture was designed to work at the read-only level without changing its underlying data sources, since this mainly concerns grouping, filtering and navigation for data collection.

However, if you want to know exactly why, you may have to talk to someone from Microsoft who worked on this part of the framework.

+3
source

Source: https://habr.com/ru/post/1440423/


All Articles