I am new to WPF and looks like I do not understand C# completely either.
The code below is supposed to provide sorted data to DataGrid.
Here is the code I struggle to understand:
ObservableCollection<Person> PersonsCollection = new ObservableCollection<Person>();
//this one is easy: I create new collection for objects of class Person and I call it PersonsCollection
ICollectionView PersonsView = CollectionViewSource.GetDefaultView(PersonsCollection);
//this one is more complicated. Does it mean that I create new object called PersonsView which I assume that implements ICollectionView interface?
ListCollectionView personsView = PersonsView as ListCollectionView;
//this one I do not understand. Why do we need it? How can we treat PersonsView as ListCollectionView?
personsView.CustomSort = new PersonSorter();
//here we assign object of PersonSorter class to do the sorting. Fine with me.
dataGrid1.ItemsSource = personsView;
//and here we assign personsView as a ItemsSource for our DataGrid. Fine with me.
Any help? Thank you :-)
this one is easy: I create new collection for objects of class Person and I call it PersonsCollection.
Correct, but I wanted to clear a few things first. You can use any collection here, or to be more precise any IEnumerable
.
What differentiates an ObservableCollection
from a normal IEnumerable
is that the first one raises notifications when items are added, removed or re-ordered in the collection while the latter doesn't.
IMPORTANT: One thing to notice is that no matter what type of collection, whether an IEnumerable
or an ObservableCollection
, when that collection is used in bindings then the WPF system creates a wrapper around that collection (source), sort of like a default view.
That view implements ICollectionView
. It keeps notion of the current item and provides capabilities like sorting, navigation, filtering and grouping.
This view is related to the collection (source) so if you have multiple bindings to that same collection then all of those bindings actually bind to the default view created by the WPF system so they are updated together when the default view is updated.
I had to clear that last important topic because it relates to the questions ahead.
this one is more complicated. Does it mean that I create new object called PersonsView which I assume that implements
ICollectionView
interface ?
No or at least not entirely true. You you are getting the reference to that default view that will be created by the WPF system, that's why the method to obtain that object is called GetDefaultView()
and not something like CreateDefaultView()
.
this one I do not understand. Why do we need it? How can we treat PersonsView as ListCollectionView?
We don't really need it, we can go without this line. We can treat PersonView
as a ListCollectionView
because
all collections have a default CollectionView. For all collections implementing IList, the ListCollectionView object is the default view object.
The rest of it is the fine with you and fine by me so no need to comment.