【问题标题】:Prevent ComboBox from updating displayed text when datasource changes防止 ComboBox 在数据源更改时更新显示的文本
【发布时间】:2016-03-26 08:46:43
【问题描述】:

我有一个绑定到 BindingList 的 ComboBox。 背景是我希望有一个从下拉列表中选择的选项(作为建议),但是如果 ItemsSource 更改(例如,当我将项目添加到绑定列表)。

有没有办法防止 ComboBox 在项目源更改时更新显示的文本?

一些代码:

this.comboBox1.DataSource = database.ListItems; // database.ListItems is of type BindingList<string>

public void update_ListItems(BindingList<string> ListItems)
{
    ListItems.Add("Item"); // Causes an update of the displayed text in the ComboBox 
}

【问题讨论】:

    标签: c# winforms combobox datasource bindinglist


    【解决方案1】:

    如果您需要打破 ComboBox 和数据源之间的绑定,您可以像这样将 BindingList 转换为 List:

    this.comboBox1.DataSource = database.ListItems.ToList();
    

    这将防止在您更改 BindingList 时更新 ComboBox 项

    【讨论】:

      猜你喜欢
      • 2019-09-11
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 2012-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多