Adam Miles

Hey,

This really should be incredibly easy to do but all I can find is huge explanations of how to use CollectionViews, Observable whatnots, SortDescriptions etc.

In code, I want to be able to add ["Cat", "Dog", "Ant", "Bat"] to a ListBox, then be able to sort them... I've looked at SortDescriptions but have no idea what the first argument needs to be. Exceptions are thrown when i try things like "Content", "Name", "Text"... what properties to simple strings have

I really do want to avoid XAML for this bit as it isn't ideal, code, a couple of lines...

Thanks,

Adam Miles



Re: Windows Presentation Foundation (WPF) Sorting a ListBox of strings

JoyK

Hi ,Try like this.

void Click(object sender, RoutedEventArgs arg){

ListBoxItem l1 = new ListBoxItem();l1.Content = "Cat";Lstbx.Items.Add(l1);

ListBoxItem l2 = new ListBoxItem();l2.Content = "dog";Lstbx.Items.Add(l2);

ListBoxItem l4 = new ListBoxItem();l4.Content = "Bat";Lstbx.Items.Add(l4);

ListBoxItem l3 = new ListBoxItem();l3.Content = "Elephant";Lstbx.Items.Add(l3);

Lstbx.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("Content", System.ComponentModel.ListSortDirection.Descending));

}

Dont use LstBx.Items.Add("Dog"); //It didnt sort when I used.Dont know y not working if we add directly without ListBoxItem.

Regards,

Joy






Re: Windows Presentation Foundation (WPF) Sorting a ListBox of strings

Sam Bent - MSFT

Just use a SortDescription whose PropertyName is empty (or null). That tells WPF to sort by the items themselves, as opposed to some property of the items. You can do this all in XAML, like this:

<CollectionViewSource Source="{StaticResource MyStrings}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription/> <!-- PropertyName defaults to null, Direction defaults to Ascending -->
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>






Re: Windows Presentation Foundation (WPF) Sorting a ListBox of strings

Miamix

Hi, guys!

I have the same question. But only to sort ComboBox, whose items are simply strings.
The code
mycombobox.Items.SortDescription.Add( new System.ComponentModel.SortDescription("Content", System.ComponentModel.ListSortDirection.Ascending);

does not really sort the items.

I haven't understand the suggestion to set the first parameter to null. Because the constructor will throw a NullArgumentExcpetion.
Can't we haven an easy way to have our ItemsControl that contains only strings sorted

Thanks




Re: Windows Presentation Foundation (WPF) Sorting a ListBox of strings

Sam Bent - MSFT

The NullArgumentException is a bug (fixed in Orcas). There's an easy workaround: use the default constructor.

Code Snippet
mycombobox.Items.SortDescriptions.Add( new System.ComponentModel.SortDescription() );

This sets the property name to null, meaning "use the item itself as its own key", and sorts ascending. If you want to sort descending, set the direction property after construction:

Code Snippet

SortDescription sd = new SortDescription();

sd.Direction = ListSortDirection.Descending;

mycombobox.Items.SortDescriptions.Add( sd );