Re-sort WPF DataGrid after bounded Data has changed Re-sort WPF DataGrid after bounded Data has changed wpf wpf

Re-sort WPF DataGrid after bounded Data has changed


It took me the whole afternoon but I finally found a solution that is surprisingly simple, short and efficient:

To control the behaviors of the UI control in question (here a DataGrid) one might simply use a CollectionViewSource. It acts as a kind of representative for the UI control inside your ViewModel without completely breaking the MVMM pattern.

In the ViewModel declare both a CollectionViewSource and an ordinary ObservableCollection<T> and wrap the CollectionViewSource around the ObservableCollection:

// Gets or sets the CollectionViewSourcepublic CollectionViewSource ViewSource { get; set; }// Gets or sets the ObservableCollectionpublic ObservableCollection<T> Collection { get; set; }// Instantiates the objets.public ViewModel () {    this.Collection = new ObservableCollection<T>();    this.ViewSource = new CollectionViewSource();    ViewSource.Source = this.Collection;}

Then in the View part of the application you have nothing else to do as to bind the ItemsSource of the CollectionControl to the View property of the CollectionViewSource instead of directly to the ObservableCollection:

<DataGrid ItemsSource="{Binding ViewSource.View}" />

From this point on you can use the CollectionViewSource object in your ViewModel to directly manipulate the UI control in the View.

Sorting for example - as has been my primary problem - would look like this:

// Specify a sorting criteria for a particular columnViewSource.SortDescriptions.Add(new SortDescription ("columnName", ListSortDirection.Ascending));// Let the UI control refresh in order for changes to take place.ViewSource.View.Refresh();

You see, very very simple and intuitive. Hope that this helps other people like it helped me.


This is more for clarification than it is an answer, but WPF always binds to an ICollectionView and not the source collection. CollectionViewSource is just a mechanism used to create/retrieve the collection view.

Here's a great resource about the topic which should help you make better use of collection views in WPF: http://bea.stollnitz.com/blog/?p=387

Using CollectionViewSource in XAML can actually simplify your code some:

<Window.Resources>    <CollectionViewSource Source="{Binding MySourceCollection}" x:Key="cvs">      <CollectionViewSource.SortDescriptions>        <scm:SortDescription PropertyName="ColumnName" />      </CollectionViewSource.SortDescriptions>    </CollectionViewSource></Window.Resources>...<DataGrid ItemsSource="{Binding Source={StaticResource cvs}}"></DataGrid>

Some people argue that when following the MVVM pattern, the view model should always expose the collection view but in my opinion, it just depends on the use case. If the view model is never going to directly interact with the collection view, it's just easier to configure it in XAML.


The answer by sellmeadog is either overly complicated or out of date. It's super simple. All you have to do is:

<UserControl.Resources>    <CollectionViewSource         Source="{Binding MyCollection}"         IsLiveSortingRequested="True"         x:Key="MyKey" /></UserControl.Resources><DataGrid ItemsSource="{Binding Source={StaticResource MyKey} }" >...