1

My question is related to that post, but I create a new post as the question is very old.

I am beginning to use ICollectionView, so I made the ViewModel as the following :

public ICollectionView SampleListView { get; set; }
SampleListView = new CollectionViewSource() { Source = SampleList }.View;

The problem is when I change the source, I used to do the following :

List<T> list= GetMyList();
SampleList=new ObservableCollection<T>(list);

The accepted answer (by blindmeis) explains that there are two options to solve that issue :

1) Instead of replacing the ObservableCollection you can use .Clear() and add the items from the new Collection. This has the additional bonus of not breaking your grouping and sorting.

2) *whenever you replace your ObservableCollection you have to create a new ICollectionView for sorting and grouping.

this._view = (ICollectionView)CollectionViewSource.GetDefaultView(this.MyCollection);

You can simply bind to your collection if you take the DefaultView

<DataGrid Name="grid" ItemsSource="{Binding MyCollection}" />

My question is, what is the best way to do that? I mean the "clean" solution?

For me the faster would be to use solution two(only because I have a big project with lot of ListViews, and would be a big work to take option 1). What are the advantages of taking the option 1? Is it worth it?(except keeping grouping/sorting).

Siegfried.V
  • 1,508
  • 1
  • 16
  • 34
  • What "issue" are you trying to solve? If you want to replace the entire collection for whatever reason, why don't you? – mm8 Apr 02 '19 at 12:45
  • Just learned recently about ICollectionView(easier to apply some filters, sort...), so I am trying to apply them on my project, and I got in the same situation as the person who made that old post. 2nd solution seemed much easier to apply, but finally I decided to use 1st solution that seems more correct way? In fact I believed this would be very long to change everything, but it was faster than expected, will finish tomorrow I guess. – Siegfried.V Apr 02 '19 at 15:52

0 Answers0