简体   繁体   English

如何将ComboBox文本设置为不在DataSource(C#)中的值?

[英]How can I set ComboBox text to a value not in DataSource (C#)?

I have multiple DropDown-style ComboBoxes on a form in which the user is able to specify which units are associated with a value (think meters, feet, etc.). 我在窗体上具有多个DropDown样式的ComboBox,用户可以在其中指定与值(例如,米,英尺等)关联的单位。 To do this, I use an application string collection in Settings.Settings with some default unit types which is bound to the DataSource of the ComboBox as follows: 为此,我在Settings.Settings中使用一个应用程序字符串集合,并将其与一些默认单位类型绑定到ComboBox的DataSource,如下所示:

this.cboUnit.DataSource = 
    (System.Collections.Specialized.StringCollection)Properties.Settings.Default.Units;

The user is able to successfully pick a unit or specify a new one. 用户能够成功选择一个单位或指定一个新单位。

Now, when I save this.cboUnit.Text into some XML profile, I am able to handle both cases. 现在,当我将this.cboUnit.Text保存到某些XML配置文件中时,我能够处理两种情况。 Opening the XML profile and performing this.cboUnit.Text = "NonExistentUnit"; 打开XML配置文件并执行this.cboUnit.Text = "NonExistentUnit"; will fail and cause the first item in the collection to be selected instead. 将失败,并导致选择集合中的第一项。

How can I modify the ComboBox to support the kind of behavior I'm asking, without adding the item to the ComboBox or modifying the DataSource itself ? 如何在不将项目添加到ComboBox或修改DataSource本身的情况下修改ComboBox以支持我要求的行为?

There was a solution suggested in Using ComboBox.Text when combobox is tied to DataSource but I couldn't find official documentation to support this behavior. 当combobox绑定到DataSource时,Using ComboBox.Text中提出了一个解决方案,但我找不到支持此行为的官方文档。 For that reason I prefer to avoid it. 因此,我宁愿避免这种情况。

I could not find any valid way to insert items into a bound DataSource. 我找不到将项目插入绑定的DataSource中的任何有效方法。 So instead I ended up doing the following: 因此,我最终做了以下工作:

BindingSource bindingSource = new BindingSource((System.Collections.Specialized.StringCollection)Properties.Settings.Default.Units, "");
if(!bindingSource.Contains(someSavedValue))
{
    bindingSource.Insert(0, someSavedValue));
}
this.cboUnit.DataSource = bindingSource;

This creates a new instance of the binding source that can be modified prior to binding it to a data source. 这将创建绑定源的新实例,可以在将其绑定到数据源之前对其进行修改。 I wrapped this code in a method for which I can pass in a string collection and the stored value ( savedUnit in this case) that returns a new BindingSource instance. 我将此代码包装在一个方法中,该方法可以传递一个字符串集合和一个存储的值(在本例中为savedUnit ),该值返回一个新的BindingSource实例。 This is especially useful in my case, as I can now simply write: 这对我来说特别有用,因为我现在可以简单地写:

this.cboLengthUnit.DataSource = CreateBindingSource(unitsCollection, savedLengthUnit);
this.cboWidthUnit.DataSource = CreateBindingSource(unitsCollection, savedWidthUnit);
this.cboHeightUnit.DataSource = CreateBindingSource(unitsCollection, savedHeightUnit);

I wrote the code above from memory, so it may contain a mistake. 我是从内存中编写上述代码的,因此可能包含错误。

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

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