Change list view sort Property / Direction within XAML only

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?


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">
            <scm:SortDescription PropertyName="{Binding Path=SortPropertyName, Source=comboSort}"/>

    <ListView ItemsSource="{Binding Source={StaticResource cvs}}"  />


you can this all at code behind in your viewmodel

// in your view model
private void ChangeSorting () {
  var collView = CollectionViewSource.GetDefaultView(ListValues);
  // do this one
  collView.SortDescriptions.Add(new SortDescription("YourPropertyName", ListSortDirection.Ascending));
  // or this one
  collView.SortDescriptions.Add(new SortDescription("YourOtherPropertyName", ListSortDirection.Descending));

public ICollectionView ListValuesCollectionViewSource
  get {
    return collView;

<ListView ItemsSource="{Binding viewModel.ListValuesCollectionViewSource}"  />


here is a little example for your view model

<ComboBox ItemsSource="{Binding viewmodel.YourDataForComboboxCollection, Mode=OneWay}"
          SelectedItem="{Binding viewmodel.SelectedCombobox}" />

a little viewmodel

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;

namespace YourNameSpace
  public class ViewModel : INotifyPropertyChanged
    public static readonly DependencyProperty SelectedComboboxProperty =
      DependencyProperty.Register("SelectedCombobox", typeof(YourDataForCombobox), typeof(ViewModel), new PropertyMetadata(default(YourDataForCombobox), new PropertyChangedCallback(SelectedComboboxCallback)));

    private static void SelectedComboboxCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) {
      var vm = sender as ViewModel;
      if (vm != null && e.NewValue != null && e.NewValue != e.OldValue) {

    public ViewModel() {
      this.YourDataForComboboxCollection = new ObservableCollection<YourDataForCombobox>();

    private void ChangeSorting(YourDataForCombobox newValue) {
      this.yourCollectionView.SortDescriptions.Add(new SortDescription(newValue.PropertyName, newValue.Sorting));

    private IObservableCollection yourDataForComboboxCollection;

    public IObservableCollection YourDataForComboboxCollection {
      get { return this.yourDataForComboboxCollection; }
      set {
        this.yourDataForComboboxCollection = value;

    public YourDataForCombobox SelectedCombobox {
      get { return (YourDataForCombobox)GetValue(SelectedComboboxProperty); }
      set { SetValue(SelectedComboboxProperty, value); }

    private IObservableCollection yourCollection;
    private ICollectionView yourCollectionView;

    public ICollectionView YourCollectionView {
      get { return this.GetCollectionView(); }

    private ICollectionView GetCollectionView() {
      if (this.yourCollection == null) {
        this.yourCollection = new ObservableCollection<YourDataForCollection>();
        this.yourCollectionView = CollectionViewSource.GetDefaultView(this.yourCollection);
        // initial sorting
      return this.yourCollectionView;

    private void RaisePropertyChanged(string property) {
      var eh = this.PropertyChanged;
      if (eh != null) {
        eh(this, new PropertyChangedEventArgs(property));

    public event PropertyChangedEventHandler PropertyChanged;

hope this helps

You can also put this into a behavior, adding another property to bind to to dynamically set the sort description direction, but this solution only works for sorting by one property. It could certainly be expanded to work for more.


    <CollectionViewSource x:Key="GroupedMeetingItems" Source="{Binding Items}" util:CollectionViewSourceBehavior.IsAscending="{Binding IsItemsAscending}">
            <PropertyGroupDescription PropertyName="StartDateTime" Converter="{StaticResource DateTimeToDisplayDateConverter}" />
            <scm:SortDescription PropertyName="StartDateTime" Direction="Descending"/>


public static class CollectionViewSourceBehavior
    public static readonly DependencyProperty IsAscendingProperty =
            new UIPropertyMetadata(false, OnIsAscendingChanged));

    public static object GetIsAscending(FrameworkElement element)
        return element.GetValue(IsAscendingProperty);

    public static void SetIsAscending(FrameworkElement element, object value)
        element.SetValue(IsAscendingProperty, value);

    public static void OnIsAscendingChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        var collectionViewSource = dependencyObject as CollectionViewSource;
        if (collectionViewSource == null)

        var isAscending = e.NewValue as bool? == true;
        var newSortDescription = new SortDescription
                Direction = isAscending ? ListSortDirection.Ascending : ListSortDirection.Descending,
                PropertyName = collectionViewSource.SortDescriptions.FirstOrDefault().PropertyName

Need Your Help

UDP recvfrom() causing issues

c++ visual-studio networking udp

I have been trying to receive a data packet from a client however during the recvfrom() process I think im doing something wrong...

Separating What from How - Design in an Agile Environment

agile scrum product-management

In an agile (scrum) environment, how do you get product management to create small enough backlog items or stories without having them do all the design, which is not their specialty? In other wor...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.