What are the differences between CollectionViewSource, ICollectionView, ListCollectionView, IList and BindingListCollectionView and their use cases?
Asked Answered
H

1

9

What are the differences between CollectionViewSource, ICollectionView, ListCollectionView, IList and BindingListCollectionView?
When and where to use all these collections?

I know the main use of CollectionViewSource, but I'm not clear with when to use these, because in XAML I use CollectionViewSource for grouping, sorting, etc. and when I want it in code-behind I use ListCollectionView.

Could you explain the exact differences and the use case(s) of each type of collection?

Hartnett answered 20/6, 2013 at 12:54 Comment(3)
Do you really need IList explained?Ussery
@jon , I know It Represents a non-generic collection of objects that can be individually accessed by index but my question is can't i achieve this using normal List(Of T), perhaps i wuld be wrong ,if u explain when and where and what is the differences betrween these things it wuld be helpfull for me better understanding. ThnksHartnett
@Hartnett generic List is a specific implementation of IList.Dupree
M
2

Basically, the CollectionView is a means of building a Master Ditail View in the simplest possible way in WPF. First of all, I would like to introduce you to the fallback mechanism.

<Window.Resources>
        <x:Array x:Key="planets" Type="{x:Type local:Planets}">
            <local:Planets Name="Earth" Diameter="12,756 km" Mass="5.97 10^24kg" Density="5514 kg/m³"/>
            <local:Planets Name="Mars" Diameter="6792 km" Mass="0.642 10^24kg" Density="3933 kg/m³"/>
            <local:Planets Name="Jupiter" Diameter="142,984 km" Mass="1898 10^24kg" Density="1326 kg/m³"/>
        </x:Array>
    </Window.Resources>
    <DockPanel DataContext="{StaticResource planets}">
        <ListBox ItemsSource="{Binding}"
                 IsSynchronizedWithCurrentItem="True"
                 DisplayMemberPath="Name" Width="125"/>
        <StackPanel>
            <TextBlock Text="{Binding Name}"/>
            <TextBlock Text="{Binding Diameter}"/>
            <TextBlock Text="{Binding Mass}"/>
            <TextBlock Text="{Binding Density}"/>
        </StackPanel>
    </DockPanel>
public class Planets
    {
        public string Name { get; set; }
        public string Diameter { get; set; }
        public string Mass { get; set; }
        public string Density { get; set; }
    }

We have a simple array of type Planets. The DisplayMemberPath-Property is also Name set, so the planet name is displayed in the ListBox. In addition to the ListBox, the details of the respective planet are displayed in a TextBlock. Vourla, there we have our working master detail view with implicit use of the CollectionView via the fallback mechianism

enter image description here

Since the property IsSynchronizedWithCurrentItem="True" is set, the WPF generates a Default-CollectioView in the background and saves the object selected in the ListBox in the CurrentItem-Property. It is now the case that the Binding of the TextBlock first searches for the Property on the Planets[] Array; it doesn’t find anything there, but the fallback mechanism forwards it to the CurrentItem-Property of the Default-CollectionView. It is also possible to reference the CurrentItem-Property of the Default-CollectionView directly. To do this, do a simple (/) in front of the Property in the Binding.

<TextBlock Text="{Binding /Name}"/>

The fallback mechanism only works if the Path-Property of the Binding is set.

CollectionViewSource is derived from CollectionView and is a proxy to provide certain Properties such as Source or View.

There are three other classes derived from CollectionView, these are ItemCollection, ListCollectionView and BindingListCollectionView. The Interfaces of the Classes, i.e. the types in front of them (I), are there to avoid a direct coupling with the Classes. The principle that is followed is called Dependencie Injection

The three Classes ItemCollection, ListCollectionView and BindingListCollectionView are designed for different purposes. ItemCollection thus provides an IEnumerable, ListCollectionView an IList and BindingListCollectionView an IBindingList Object. Thus, different types are provided for different purposes.

All in all, the CollectionView is a powerful WPF tool; you can sort, group, etc. and display details.

I hope this helps you further. Greetings Michael

Milano answered 3/12, 2021 at 22:14 Comment(1)
This is a bit of a disadvantage, but I have now first seen that the question is from 2013. Sorry for that, I'm just a beginner. I hope somebody continues anyway.Milano

© 2022 - 2024 — McMap. All rights reserved.