How can I sort a ListBox using only XAML and no code-behind?

31,697

Use a CollectionViewSource:

<CollectionViewSource x:Key="SortedItems" Source="{Binding CollectionOfStrings}"
    xmlns:scm="clr-namespace:System.ComponentModel;assembly=Win‌​dowsBase">
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="SomePropertyOnYourItems"/>
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

<ListBox ItemsSource="{Binding Source={StaticResource SortedItems}}"/>

You might want to wrap your strings in a custom VM class so you can more easily apply sorting behavior.

Share:
31,697
Eben Geer
Author by

Eben Geer

Updated on July 09, 2022

Comments

  • Eben Geer
    Eben Geer almost 2 years

    I need to sort the strings in a ListBox, but it is bound to the view model by another component via the DataContext. So I can't directly instantiate the view model in XAML, as in this example, which uses the ObjectDataProvider.

    In my XAML:

    <ListBox ItemsSource="{Binding CollectionOfStrings}" />
    

    In my view model:

    public ObservableCollection<string> CollectionOfStrings
    {
        get { return collectionOfStrings; }
    }
    

    In another component:

    view.DataContext = new ViewModel();
    

    There is no code behind! So using purely XAML, how would I sort the items in the ListBox? Again, the XAML doesn't own the instantiation of the view model.

  • Eben Geer
    Eben Geer almost 15 years
    Thanks, Kent! Binding the Source attribute on a CollectionViewSource was the missing link for me. I appreciate it. In this case, I didn't want a custom VM class, so I just left off the PropertyName attribute, which apparently works for string collections just fine.
  • Eben Geer
    Eben Geer almost 15 years
    Also, to any onlookers out there, the SortDescription tag takes a Direction attribute.
  • Ingó Vals
    Ingó Vals over 13 years
    What if the ListBox is a part of a DataTemplate representing a property of a object that is a list of items. Can't I do the sorting inside the ListBox somehow?
  • Ingó Vals
    Ingó Vals almost 13 years
    Can you tell us where the smc namespace looks like. I'm not succesfully finding SortDescription using Xaml.
  • Shadow The Kid Wizard
    Shadow The Kid Wizard almost 13 years
    Comment by franssu: scm includes the "System.ComponentModel" namespace from the WindowsBase assembly. (xmlns:scm="clr-namespace:System.ComponentModel;assembly=Win‌​dowsBase")
  • Konstantin Salavatov
    Konstantin Salavatov over 10 years
    for Silverlight xmlns:scm="="clr-namespace:System.ComponentModel;assembly=Sy‌​stem.Windows"
  • RobM
    RobM over 4 years
    There seems to be some hidden characters in the code snippet. Copying it to notepad++ shows this for example 'Win??dowsBase' I had the SortOnDescription issue also. The characters don't show up in blend. Retyping 'WindowsBase' fixed it for me. I would edit the snippet but it complains that I need to edit more than 6 characters.