在c# WinApp中,我如何同时添加文本和值到我的组合框的项目? 我做了一个搜索,通常答案是使用“绑定到一个源”..但在我的情况下,我没有一个绑定源准备在我的程序… 我怎么做这样的事情:

combo1.Item[1] = "DisplayText";
combo1.Item[1].Value = "useful Value"

当前回答

这类似于其他一些答案,但是很紧凑,如果已经有一个列表,就避免了转换到字典。

给定一个窗口窗体上的ComboBox“ComboBox”和一个具有字符串类型属性Name的类SomeClass,

List<SomeClass> list = new List<SomeClass>();

combobox.DisplayMember = "Name";
combobox.DataSource = list;

这意味着SelectedItem是list中的SomeClass对象,并且组合框中的每个项目将使用其名称显示。

其他回答

您必须创建自己的类类型并重写ToString()方法以返回所需的文本。下面是一个你可以使用的类的简单例子:

public class ComboboxItem
{
    public string Text { get; set; }
    public object Value { get; set; }

    public override string ToString()
    {
        return Text;
    }
}

下面是它使用的一个简单例子:

private void Test()
{
    ComboboxItem item = new ComboboxItem();
    item.Text = "Item text1";
    item.Value = 12;

    comboBox1.Items.Add(item);

    comboBox1.SelectedIndex = 0;

    MessageBox.Show((comboBox1.SelectedItem as ComboboxItem).Value.ToString());
}
using (SqlConnection con = new SqlConnection(insertClass.dbPath))
{
    con.Open();
    using (SqlDataAdapter sda = new SqlDataAdapter(
    "SELECT CategoryID, Category FROM Category WHERE Status='Active' ", con))
    {
        //Fill the DataTable with records from Table.
        DataTable dt = new DataTable();
        sda.Fill(dt);
        //Insert the Default Item to DataTable.
        DataRow row = dt.NewRow();
        row[0] = 0;
        row[1] = "(Selecione)";
        dt.Rows.InsertAt(row, 0);
        //Assign DataTable as DataSource.
        cboProductTypeName.DataSource = dt;
        cboProductTypeName.DisplayMember = "Category";
        cboProductTypeName.ValueMember = "CategoryID";
    }
}             
con.Close();

Visual Studio 2013是这样做的:

单一项目:

comboBox1->Items->AddRange(gcnew cli::array< System::Object^  >(1) { L"Combo Item 1" });

多个项目:

comboBox1->Items->AddRange(gcnew cli::array< System::Object^  >(3)
{
    L"Combo Item 1",
    L"Combo Item 2",
    L"Combo Item 3"
});

不需要重写类或包含任何其他内容。是的,comboBox1->SelectedItem和comboBox1->SelectedIndex调用仍然有效。

除了Adam Markowitz的回答之外,这里还有一种通用的方法(相对来说)简单地将组合框的ItemSource值设置为枚举,同时向用户显示“Description”属性。(您可能认为每个人都想这样做,这样它就会成为。net的一行程序,但事实并非如此,而且这是我发现的最优雅的方式)。

首先,创建这个简单的类,用于将任意Enum值转换为ComboBox项:

public class ComboEnumItem {
    public string Text { get; set; }
    public object Value { get; set; }

    public ComboEnumItem(Enum originalEnum)
    {
        this.Value = originalEnum;
        this.Text = this.ToString();
    }

    public string ToString()
    {
        FieldInfo field = Value.GetType().GetField(Value.ToString());
        DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
        return attribute == null ? Value.ToString() : attribute.Description;
    }
}

其次,在OnLoad事件处理程序中,您需要将组合框的源设置为基于枚举类型中的每个Enum的ComboEnumItems列表。这可以用Linq实现。然后设置DisplayMemberPath:

    void OnLoad(object sender, RoutedEventArgs e)
    {
        comboBoxUserReadable.ItemsSource = Enum.GetValues(typeof(EMyEnum))
                        .Cast<EMyEnum>()
                        .Select(v => new ComboEnumItem(v))
                        .ToList();

        comboBoxUserReadable.DisplayMemberPath = "Text";
        comboBoxUserReadable.SelectedValuePath= "Value";
    }

现在用户将从用户友好的描述列表中进行选择,但他们选择的将是您可以在代码中使用的枚举值。 要在代码中访问用户的选择,comboBoxUserReadable。SelectedItem将是ComboEnumItem和comboBoxUserReadable。SelectedValue将是EMyEnum。

您可以使用字典对象,而不是创建自定义类来在组合框中添加文本和值。

在Dictionary对象中添加键和值:

Dictionary<string, string> comboSource = new Dictionary<string, string>();
comboSource.Add("1", "Sunday");
comboSource.Add("2", "Monday");

将源Dictionary对象绑定到Combobox:

comboBox1.DataSource = new BindingSource(comboSource, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";

检索键和值:

string key = ((KeyValuePair<string,string>)comboBox1.SelectedItem).Key;
string value = ((KeyValuePair<string,string>)comboBox1.SelectedItem).Value;

完整来源:组合框文本和值