WPF: Sorting in ItemsControl

Sorting can be done via a mechanism on the ItemsCollection object, so it applies equally to all ItemsControls. ItemsCollection has a SortDescriptions property that can hold any number of System.ComponentModel.SortDescription instances. Each SortDescription describes which property of the items should be used for sorting and whether the sort is ascending or descending. For example, the following code sorts a bunch of ContentControl items based on their Content property:


// Clear any existing sorting first
myItemsControl.Items.SortDescriptions.Clear();

// Sort by the Content property
myItemsControl.Items.SortDescriptions.Add(
new SortDescription(“Content”, ListSortDirection.Ascending));