Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6899911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:28:52+00:00 2026-05-27T07:28:52+00:00

I have a simple ListView and want to sort the contents numerically or alphabetically,

  • 0

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}}"  />
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:28:53+00:00Added an answer on May 27, 2026 at 7:28 am

    you can this all at code behind in your viewmodel

    // in your view model
    private void ChangeSorting () {
      var collView = CollectionViewSource.GetDefaultView(ListValues);
      collView.SortDescriptions.Clear();
      // do this one
      collView.SortDescriptions.Add(new SortDescription("YourPropertyName", ListSortDirection.Ascending));
      // or this one
      collView.SortDescriptions.Add(new SortDescription("YourOtherPropertyName", ListSortDirection.Descending));
      collView.Refresh();
    }
    
    public ICollectionView ListValuesCollectionViewSource
    {
      get {
        return collView;
      }
    }
    
    <ListView ItemsSource="{Binding viewModel.ListValuesCollectionViewSource}"  />
    

    EDIT

    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) {
            vm.ChangeSorting(e.NewValue);
          }
        }
    
        public ViewModel() {
          this.YourDataForComboboxCollection = new ObservableCollection<YourDataForCombobox>();
        }
    
        private void ChangeSorting(YourDataForCombobox newValue) {
          this.yourCollectionView.SortDescriptions.Clear();
          this.yourCollectionView.SortDescriptions.Add(new SortDescription(newValue.PropertyName, newValue.Sorting));
          this.yourCollectionView.Refresh();
        }
    
        private IObservableCollection yourDataForComboboxCollection;
    
        public IObservableCollection YourDataForComboboxCollection {
          get { return this.yourDataForComboboxCollection; }
          set {
            this.yourDataForComboboxCollection = value;
            this.RaisePropertyChanged("YourDataForComboboxCollection");
          }
        }
    
        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
            this.ChangeSorting(null);
          }
          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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to save the state of a multiple choice listview checkbox's. I have
I have a listView with some data coming from a server.I want to do
I have a simple ListView and I want each of it items to be
I want to do a very simple thing. I have a listview in my
basically I want to encapsulate a simple component from code that I already have.
I have a rather simple ListView row: <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent android:layout_height=wrap_content
I have a simple requirement as mentioned below: A ListView or any control displays
I have an ASP ListView, and have a very simple requirement to display numbers
I have a custom listview and made overlapping very simple: in onDraw method, I
I have a simple ListView paged by a DataPager giving a list of products.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.