Установите выбранный элемент комбинированного списка на основе пары ключ-значение.
У меня есть поле со списком, который я заполняю так:
this.reqTypeInput.Items.Add(new RequestType("Label 1", "Value1"));
this.reqTypeInput.Items.Add(new RequestType("Label 2", "value2"));
this.reqTypeInput.Items.Add(new RequestType("Label 3", "value3"));
Мой класс RequestType:
class RequestType
{
public string Text { get; set; }
public string Value { get; set; }
public RequestType(string text, string val)
{
Text = text;
Value = val;
}
public override string ToString()
{
return Text;
}
}
У меня есть значение, "Value1", например. Как я могу установить selectedItem комбинированного списка на объект {Label 1, Value1}?
Я пытался:
this.reqTypeInput.SelectedIndex = this.reqTypeInput.Items.IndexOf("Value1");