c# – ComboBox在更改DataSource后记住SelectedIndex

背景

最近,我观察到Winform ComboBox控件的两个不良行为:

>将DataSource属性设置为新对象会将SelectedIndex值设置为0
>将DataSource属性设置为以前使用的对象“记住”以前的SelectedIndex值

以下是一些示例代码来说明这一点:

private void Form_Load(object sender, EventArgs e)
    {
        string[] list1 = new string[] { "A", "B", "C" };
        string[] list2 = new string[] { "D", "E", "F" };

        Debug.Print("Setting Data Source: list1");
        comboBox.DataSource = list1;

        Debug.Print("Setting SelectedIndex = 1");
        comboBox.SelectedIndex = 1;

        Debug.Print("Setting Data Source: list2");
        comboBox.DataSource = list2;

        Debug.Print("Setting SelectedIndex = 2");
        comboBox.SelectedIndex = 2;

        Debug.Print("Setting Data Source: list1");
        comboBox.DataSource = list1;

        this.Close();
    }

    private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        Debug.Print("Selected Index Changed, SelectedIndex: {0}", comboBox.SelectedIndex);
    }

    private void comboBox_DataSourceChanged(object sender, EventArgs e)
    {
        Debug.Print("Data Source Changed, SelectedIndex: {0}", comboBox.SelectedIndex);
    }

这会产生以下输出:

Setting Data Source: list1
Data Source Changed, SelectedIndex: -1
Selected Index Changed, SelectedIndex: 0
Setting SelectedIndex = 1
Selected Index Changed, SelectedIndex: 1
Setting Data Source: list2
Data Source Changed, SelectedIndex: 1
Selected Index Changed, SelectedIndex: 0
Setting SelectedIndex = 2
Selected Index Changed, SelectedIndex: 2
Setting Data Source: list1
Data Source Changed, SelectedIndex: 2
Selected Index Changed, SelectedIndex: 1

这是最后一个特别有趣的调试语句.

问题

这怎么可能,反正是为了防止这种行为?

ComboBox的“内存”是不确定的,还是受到易受垃圾收集影响的对象的支持?前一种情况意味着调整DataSource结果的内存消耗,后一种情况表明ComboBox的行为在设置DataSource时是不可预测的.

最佳答案 我不知道DataSource对象的所有内部工作方式,但它可能是ComboBox保留列表的关联CurrencyManager信息,使其能够在重新连接DataSource时记住先前的位置.

您可以通过将列表包装在新的BindingSource对象中来避免此行为:

comboBox.DataSource = new BindingSource(list1, null);

这将默认position属性返回零(如果有记录).

点赞