简体   繁体   English

更改DataSource后,ComboBox会记住SelectedIndex

[英]ComboBox remembers SelectedIndex after changing DataSource

Background 背景

Lately, I have observed two undesirable behaviors of the Winform ComboBox control: 最近,我观察到Winform ComboBox控件的两个不良行为:

  1. Setting the DataSource property to a new object sets the SelectedIndex value to 0 DataSource属性设置为新对象会将SelectedIndex值设置为0
  2. Setting the DataSource property to a previously used object "remembers" the previously SelectedIndex value DataSource属性设置为以前使用的对象“记住”以前SelectedIndex

Here is some sample code to illustrate this: 以下是一些示例代码来说明这一点:

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);
    }

This produces the following output: 这会产生以下输出:

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

It is this last debug statement that is particularly interesting. 这是最后一个特别有趣的调试语句。

Questions 问题

How is this possible and is there anyway to prevent this behavior? 这怎么可能,反正是为了防止这种行为?

Is the ComboBox's "memory" indefinite, or is it supported by objects which are susceptible to garbage collection? ComboBox的“内存”是不确定的,还是受到易受垃圾收集影响的对象的支持? The former case means adjusting DataSource results in memory consumption, the latter case indicates the ComboBox's behavior is not predictable when setting the DataSource. 前一种情况意味着调整DataSource结果的内存消耗,后一种情况表明ComboBox的行为在设置DataSource时是不可预测的。

I don't know all of the inner workings of the DataSource object, but it's probably the ComboBox keeping the associated CurrencyManager information for the list that allows it to remember the previous position when the DataSource gets reconnected. 我不知道DataSource对象的所有内部工作方式,但它可能是ComboBox保留列表的关联CurrencyManager信息,使其能够在重新连接DataSource时记住先前的位置。

You can avoid this behavior by wrapping the list inside a new BindingSource object: 您可以通过将列表包装在新的BindingSource对象中来避免此行为:

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

This will default the position property back to zero (if there are records). 这将默认position属性返回零(如果有记录)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM