Link to home
Start Free TrialLog in
Avatar of countrymeister
countrymeister

asked on

How to orderby items in a silverlight combo box

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"
Avatar of kaufmed
kaufmed
Flag of United States of America image

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

Avatar of countrymeister
countrymeister

ASKER

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" />
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial