I have a simple ListView and want to sort the contents numerically or alphabetically, ascending or descending. The choice comes from a drop down box. I understand I can use CollectionViewSource to achieve the sorting but how can I alter the SortDescription or direction on the fly?
Update:
Ok so I have setup my CVS like so, the viewModel is what the ListView is currently bound to. I require the PropertyName to be bound to the currently selected combo box item’s property PropertyName. The combo box is bound to a custom list that expose the propertyname on which I want to sort.
It complains about the PropertyName that im attempting to use:
A ‘Binding’ cannot be set on the ‘PropertyName’ property of type
‘SortDescription’. A ‘Binding’ can only be set on a DependencyProperty
of a DependencyObject.
<CollectionViewSource Source="{StaticResource viewModel.ListValues}" x:Key="cvs">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="{Binding Path=SortPropertyName, Source=comboSort}"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
<ListView ItemsSource="{Binding Source={StaticResource cvs}}" />
you can this all at code behind in your viewmodel
EDIT
here is a little example for your view model
a little viewmodel
hope this helps