How to sort in WPF ListBox?

The WPF C # 4.0 application, see the code below, shows at 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 just went for the article Sorting ListView WPF List Items

So, what is the order in which I sort by the "Content" property, and where is the "Content" property, I wonder (I tried to change it to an arbitrary "fff" instead of "Content", getting the same results as in the second screenshot, ?

+4
source share
4 answers

Since you are sorting a list of strings, do not specify a property name (first parameter SortDescription):

 listBox1.Items.SortDescriptions.Add( new System.ComponentModel.SortDescription("", System.ComponentModel.ListSortDirection.Ascending)); 
+19
source

It's easy to sort the wpf or listbox command line - but remember to include Imports System.ComponentModel .

To sort alphabetically, simply

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

or

 MyComboBox.Items.SortDescriptions.Add( New SortDescription("", ListSortDirection.Ascending)) 
+4
source
 YOULISTBOX.Items.SortDescriptions.Clear(); YOULISTBOX.Items.SortDescriptions.Add( new System.ComponentModel.SortDescription("NAME", System.ComponentModel.ListSortDirection.Ascending)); 

so that it is updated every time

+3
source

Additional Information:

The item you are sorting can be any DependencyProperty . So let's say that you have an ObservableCollection custom class associated with the ItemsSource of the ListBox. A custom class can have any number of dependency properties, and you can use them to sort. You simply put the name of the dependency property (like string ) in the new SortDescription argument.

Adding multiple SortDescription elements to the control will sort with multiple variables.

Dependency properties can represent any type of variable, not just strings. I have an example where I sort first bool , then int and finally DateTime .

+2
source

All Articles