public class ComboboxItem {
public string Text { get; set; }
public string Value { get; set; }
public override string ToString() { return Text; }
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = comboBox1.SelectedIndex;
int selecteVal = (int)comboBox1.SelectedValue;
ComboboxItem selectedCar = (ComboboxItem)comboBox1.SelectedItem;
MessageBox.Show(String.Format("Index: [{0}] CarName={1}; Value={2}", selectedIndex, selectedCar.Text, selecteVal));
}
我正在像这样添加它们:ComboboxItem item = new ComboboxItem();
item.Text = cd.Name;
item.Value = cd.ID;
this.comboBox1.Items.Add(item);
我一直收到一个NullReferenceExeption异常,不确定原因。看起来文本显示得很好。
ComboBoxItem
类仅适用于WPF项目。 - T30