【发布时间】:2014-10-27 15:33:11
【问题描述】:
我有一个只包含一个空 ComboBox 的表单。 我将 DataSource 设置为一个空的 BindingList。 当我向 BindingList 添加某些内容时,它被选中并且 combobox1.SelectedIndex 更改,但事件 comboBox1_SelectedIndexChanged 没有引发,即使在我看来也应该如此。为什么不养?当单个项目被移除时,comboBox1_SelectedIndexChanged 被正确触发。
public partial class Form1 : Form
{
public Form1()
{
var test_ = new BindingList<int>();
InitializeComponent();
comboBox1.DataSource = test_;
Console.WriteLine(comboBox1.SelectedIndex); // -1
test_.Add(42); // BUG? no comboBox1_SelectedIndexChanged -> 0
Console.WriteLine(comboBox1.SelectedIndex); // 0
test_.Remove(42); // comboBox1_SelectedIndexChanged -> -1
Console.WriteLine(comboBox1.SelectedIndex); // -1
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Console.WriteLine("index changed " + comboBox1.SelectedIndex);
}
}
【问题讨论】:
标签: c# winforms data-binding