How To fill a combobox with SortedList

1k views Asked by At

I have, on my own created a SortedList, which I cannot bind to a combobox, I'm not doing it from XAML i'm doing it from the code:

 SortedList<int, string> AreaList = new SortedList<int, string>();
            AreaList.Add(1, "Agriculture");
            AreaList.Add(2, "Forestry");
            AreaList.Add(3, "Fruits");
            AreaList.Add(4, "Food");
            AreaList.Add(5, "Metals");
            AreaList.Add(6, "Mining");
            AreaList.Add(7, "Electricity");
            AreaList.Add(8, "Building Contracts");
            AreaList.Add(9, "Transport");
            AreaList.Add(10, "Alcohol");
            AreaList.Add(11, "Information Technologies");
            AreaList.Add(12, "Health And Social Services");
            AreaList.Add(13, "Art and Entertainement");
            AreaList.Add(14, "Hospitality Business");
            AreaList.Add(15, "Education");
            AreaList.Add(16, "Real Estate");
            AreaList.Add(17, "Sales");
            AreaList.Add(18, "Architecture");
            AreaList.Add(19, "Engineering");
            AreaList.Add(20, "Wholesale");

        comboBox1.ItemsSource = AreaList.ToList();
        comboBox1.SelectedValue = "TKey";
        comboBox1.DisplayMemberPath = "TValue";

i dont know what i'm doing wrong, its my first time with SortedLists, and I'm a begginer in WPF.

1

There are 1 answers

3
Ivan Stoev On BEST ANSWER

Not sure why you need the SortedList, but at the end (due to ToList() call) you are actually binding to List<KeyValuePair<int, string>>, so you need to set SelectedValuePath and DisplayMemberPath as follows:

comboBox1.SelectedValuePath = "Key"; // bind to KeyValuePair<int, string>.Key property
comboBox1.DisplayMemberPath = "Value";  // bind to KeyValuePair<int, string>.Value property