请看下面的代码。当应用程序启动时,SelectedIndexChanged 被调用并且 x 的类型为“Example”。但是当应用程序运行并且您选择不同的东西时 SelectedIndexChanged 会产生一个结果 x 类型的浮点数。为什么会产生不同的结果?
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var example = new List<Example>();
example.Add(new Example("A", 100f));
example.Add(new Example("B", 200f));
example.Add(new Example("C", 400f));
this.comboBox1.DataSource = example;
this.comboBox1.DisplayMember = "Description";
this.comboBox1.ValueMember = "Value";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var x = this.comboBox1.SelectedValue;
}
}
public class Example
{
public Example(string desc, float val)
{
this.Description = desc;
this.Value = val;
}
public string Description { get; set; }
public float Value { get; set; }
}
}