Search code examples
c#wpfdata-bindingitemscontrol

How to display a data bound collection of int in a sorted manner


I have a very small collection of ints that I am displaying in a custom ItemsControl. I'm not worried about performance since the collection is so small. I would like the items to be displayed in ascending order and can't figure out the best way to do so. I have successfully used a CollectionViewSource with a SortDescription to sort more complex objects, but it seems to require a property name to sort by. I realize I could keep the underlying collection sorted or wrap my ints in a reference type to use the SortDescription, but both seem overkill. Am I missing something?

I'd like to do something like this, where MyCollection is of type ObservableCollection<int>

<Grid.Resources>
    <CollectionViewSource x:Key={sortedView} Source={Binding MyCollection}>
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription Direction="Ascending" PropertyName="???" />
        </CollectionViewSource.SortDescriptions>
    <CollectionViewSource>
<Grid.Resources>

...

<ItemsControl ItemsSource={StaticResource sortedView} />

Solution

  • Simply use . as the property name:

    <scm:SortDescription Direction="Ascending" PropertyName="." />