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

1

6

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

Am I missing something?

Handset answered 18/10, 2012 at 0:16 Comment(0)
W
3

The ICollectionView is only implemented by the CollectionView class. The MSDN-documentation points out that the CollectionView should not even be instantiated in your code, but instead make use of the CollectionViewSource-object to get your collection view.

If you want to have your own collection of T returned in a CollectionView, you need to add your own collection (implementing IEnumerable) to the CollectionViewSource-object and get the CollectionView from there, for instance:

    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 of T is probably because its not designed that way. The documentation points out that the CollectionView has been designed to give a different view on a collection without changing it:

You can think of a collection view as a layer on top of a binding source collection that allows you to navigate and display the collection based on sort, filter, and group queries, all without having to manipulate the underlying source collection itself.

In that regard it makes sense that you can only view the collection, hence the name 'ViewCollection'.

I think it's not so obvious to expect ICollectionView of T as CollectionView's are not even meant to be instantiated in the first place (see the interesting warning below by the way, after adding some sorting abilities).

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

I think the architecture has been designed to work on a 'read-only' based level without changing its underlying datasources, as that is what grouping, filtering and navigating a data collection's is mainly focused on.

However, if you want to know exactly why, you'd probably have to speak with someone from Microsoft that worked on this part of the framework.

Watcher answered 21/10, 2012 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.