How to sort in a WPF ListBox?

时光毁灭记忆、已成空白 提交于 2019-12-05 12:21:41

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));
John

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))
YOULISTBOX.Items.SortDescriptions.Clear(); 
YOULISTBOX.Items.SortDescriptions.Add( new System.ComponentModel.SortDescription("NAME", System.ComponentModel.ListSortDirection.Ascending));

to ensure it update every time

Tim

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!