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).