C# 获取填充了数据源的 ComboBox 的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/867395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:50:44  来源:igfitidea点击:

Getting the items of a ComboBox with its DataSource filled

c#winforms.net-2.0controlscombobox

提问by

Consider that there is a ComboBox which is populated through its DataSource property. Each item in the ComboBox is a custom object and the ComboBox is set with a DisplayMemberand ValueMember.

考虑有一个通过其 DataSource 属性填充的 ComboBox。ComboBox 中的每个项目都是一个自定义对象,并且 ComboBox 使用DisplayMember和 进行设置ValueMember

IList<CustomItem> aItems = new List<CustomItem>();
//CustomItem has Id and Value and is filled through its constructor
aItems.Add(1, "foo"); 
aItems.Add(2, "bar");

myComboBox.DataSource = aItems;

Now the problem is that, I want to read the items as string that will be rendered in the UI. Consider that I don't know the type of each item in the ComboBox (CustomItemis unknown to me)

现在的问题是,我想将项目作为将在 UI 中呈现的字符串读取。考虑到我不知道 ComboBox 中每个项目的类型(我不知道CustomItem

Is this possible ?

这可能吗 ?

回答by Steven Evers

Binding:

捆绑:

ComboBox1.DataSource = aItems;
ComboBox1.DisplayMember = "Value";

Getting the item:

获取物品:

CustomItem ci = ComboBox1.SelectedValue as CustomItem;

edit: If all that you want to get is a list of all of the display values of the combobox

编辑:如果您想要获得的只是组合框所有显示值的列表

List<String> displayedValues = new List<String>();
foreach (CustomItem ci in comboBox1.Items)
    displayedValues.Add(ci.Value);

回答by Mitch Wheat

Create an Interface, say ICustomFormatter, and have those custom objects implement it.

例如ICustomFormatter,创建一个接口,并让这些自定义对象实现它。

interface ICustomFormatter
{
   public string ToString();
}

Then call the ToString()method.

然后调用ToString()方法。

EDIT: link to Decoratorpattern.

编辑:链接到装饰模式。

回答by Henk Holterman

You should be able to get at ValueMember and DisplayMember through reflection. But interrogating the combobox might be a little easier. The following will work, but maybe you want to surround it with SuspendUpdate or something.

您应该能够通过反射获得 ValueMember 和 DisplayMember。但是询问组合框可能更容易一些。以下将起作用,但也许您想用 SuspendUpdate 或其他东西包围它。

string s = string.Empty;
int n = comboBox1.Items.Count;

for (int i = 0; i < n; i++)
{
    comboBox1.SelectedIndex = i;
    s = s + ';' + comboBox1.Text; // not SelectedText;
}

回答by Zack

Although slightly more computationally expensive, Reflection could do what you want:

尽管计算成本稍高,反射可以做你想做的事:

using System.Reflection;    
private string GetPropertyFromObject(string propertyName, object obj)
    {
        PropertyInfo pi = obj.GetType().GetProperty(propertyName);
        if(pi != null)
        {
            object value = pi.GetValue(obj, null);
            if(value != null)
            {
                return value.ToString();
            }
        }
        //return empty string, null, or throw error
        return string.Empty;
    }