How to orderby items in a silverlight combo box

countrymeister
countrymeister used Ask the Experts™
on
I have a generic list which contains an object T.

This same list is used to bind multiple combo boxes on a silverlight page.

After I bind the combobox to this List source, I would like to order the Items in the Combobox accordingly.

Please not that I do not want to sort or order the List<T>, because this is used to bind multiple combo boxes

I would like to try something like this for my comboboxes
cbDesc.Items.OrderBy(???);


Here is my code
List<T> genericList = getData();

            cbDesc.ItemsSource = genericList
            cbDesc.DisplayMemberPath ="Name"
            cbDesc.SelectedValuePath = "Name"
            cbDesc.Items.OrderBy(???);


            cbRegion.ItemsSource = genericList
            cbRegion.DisplayMemberPath ="Region"
            cbRegion.SelectedValuePath = "Region"

            cbProduct.ItemsSource = genericList
            cbProduct.DisplayMemberPath ="Product"
            cbProduct.SelectedValuePath = "Product"
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
The OrderBy is not "in-place", so you can call it against the list without affecting the order of the elements. You should be able to do:

cbDesc.ItemsSource = cbDesc.Items.OrderBy(item => item.Name);

Open in new window

Author

Commented:
I do not see the avaialble properties when I do
cbDesc.ItemsSource = cbDesc.Items.OrderBy(item => item.Name);

All I see when I type item.   is  Equals, GetType, ToString(), GetHasCode.
I do not see any of the properties such as Name, Region, Product

My xaml for the combobox is

<ComboBox Height="20" HorizontalAlignment="Left" Margin="155,19,0,0" Name="cbDesc" VerticalAlignment="Top" MinWidth="349" SelectionChanged="cbDesc_SelectionChanged" MaxDropDownHeight="200" />
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
That's my fault. I intended on putting the list. Let's chalk it up to copy/paste error  : \

Corrected:
cbDesc.ItemsSource = genericList.OrderBy(item => item.Name);

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial