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.
Not sure why you need the
SortedList
, but at the end (due toToList()
call) you are actually binding toList<KeyValuePair<int, string>>
, so you need to setSelectedValuePath
andDisplayMemberPath
as follows: