10

The C# 4.0 WPF application, see the code below, shows on startup:

Initial order of in <code>ListBox</code>

abd after clicking the Sort button with btnSort_Click() click event handler:

ListBox After Sorting

How can I sort in order aaa, bbb, ccc?

C# code:

public MainWindow()
{
  InitializeComponent();

  listBox1.Items.Add("ccc");
  listBox1.Items.Add("aaa");
  listBox1.Items.Add("bbb");
}
private void btnSort_Click(object sender, RoutedEventArgs e)
{
  listBox1.Items.SortDescriptions.Add(
  new System.ComponentModel.SortDescription("Content",
       System.ComponentModel.ListSortDirection.Ascending));
}
private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
  listBox1.Items.RemoveAt
     (listBox1.Items.IndexOf(listBox1.SelectedItem));
}

XAML:

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox Height="100" HorizontalAlignment="Left" Margin="8,43,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" MouseDoubleClick="listBox1_MouseDoubleClick" />
        <Button Content="Sort" Height="23" HorizontalAlignment="Left" Margin="140,94,0,0" Name="btnSort" VerticalAlignment="Top" Width="75" Click="btnSort_Click" />
    </Grid>
</Window>

Update:
Well, I simply followed the article "Sorting a WPF ListBox Items"

So, what is the order by which I am sorting by a property "Content" and where is that property "Content", I wonder (tried to change it to arbitrary "fff" instead of "Content" having gotten the same, as in 2nd screenshot, results?

4 Answers 4

28

Since you're sorting a list of strings, don't indicate a property name (first parameter of SortDescription):

listBox1.Items.SortDescriptions.Add(
            new System.ComponentModel.SortDescription("",
            System.ComponentModel.ListSortDirection.Ascending));
4
  • I'm not sure what exactly you're asking now... Did changing the code to my suggestion fix this issue?
    – Blachshma
    Mar 24, 2013 at 15:37
  • It did work. I'd like to understand what is the 1st argument of SortDescription() method for and why it doesn't give an error after providing arbitrary propertyName + why is the order changes at all (and according to which sorting rules)?
    – Fulproof
    Mar 24, 2013 at 15:46
  • Well, it does give an error. If you use an arbitrary propertyname such as fff and open your output window in Visual Studio, you'll see a binding error such as: System.Windows.Data Error: 40 : BindingExpression path error: 'fff' property not found on 'object' ''String' (HashCode=536926232)'.
    – Blachshma
    Mar 24, 2013 at 15:49
  • Thanks. I failed to notice that error! But if it gives an error why the order of items is changed, I assume being re-sorted?
    – Fulproof
    Mar 24, 2013 at 16:52
7
YOULISTBOX.Items.SortDescriptions.Clear(); 
YOULISTBOX.Items.SortDescriptions.Add( new System.ComponentModel.SortDescription("NAME", System.ComponentModel.ListSortDirection.Ascending));

to ensure it update every time

5

It's easy to sort a wpf combobox or listbox - but remember to include Imports System.ComponentModel.

To sort in alphabetical order, simply

MylistBox.Items.SortDescriptions.Add(
    New SortDescription("", ListSortDirection.Ascending))

or

MyComboBox.Items.SortDescriptions.Add(
    New SortDescription("", ListSortDirection.Ascending))
3

Additional info:

The item that you sort with may be any DependencyProperty. So lets say that you have an ObservableCollection of a custom class that is bound to the ItemsSource of the ListBox control. The custom class can have any number of dependency properties, and you can use those for the sort(s). You just put the name of the dependency property (as a string) in the new SortDescription argument.

Adding multiple SortDescriptions to the control will do a multi-variable sort.

The dependency properties may represent any type of variable and not just strings. I have an example where I am sorting first by a bool, then by an int, and finally by DateTime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.