在c# WinApp中,我如何同时添加文本和值到我的组合框的项目? 我做了一个搜索,通常答案是使用“绑定到一个源”..但在我的情况下,我没有一个绑定源准备在我的程序… 我怎么做这样的事情:
combo1.Item[1] = "DisplayText";
combo1.Item[1].Value = "useful Value"
在c# WinApp中,我如何同时添加文本和值到我的组合框的项目? 我做了一个搜索,通常答案是使用“绑定到一个源”..但在我的情况下,我没有一个绑定源准备在我的程序… 我怎么做这样的事情:
combo1.Item[1] = "DisplayText";
combo1.Item[1].Value = "useful Value"
当前回答
//set
comboBox1.DisplayMember = "Value";
//to add
comboBox1.Items.Add(new KeyValuePair("2", "This text is displayed"));
//to access the 'tag' property
string tag = ((KeyValuePair< string, string >)comboBox1.SelectedItem).Key;
MessageBox.Show(tag);
其他回答
这类似于其他一些答案,但是很紧凑,如果已经有一个列表,就避免了转换到字典。
给定一个窗口窗体上的ComboBox“ComboBox”和一个具有字符串类型属性Name的类SomeClass,
List<SomeClass> list = new List<SomeClass>();
combobox.DisplayMember = "Name";
combobox.DataSource = list;
这意味着SelectedItem是list中的SomeClass对象,并且组合框中的每个项目将使用其名称显示。
您可以使用此代码将一些项插入到具有文本和值的组合框中。
C#
private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
combox.Items.Insert(0, "Copenhagen");
combox.Items.Insert(1, "Tokyo");
combox.Items.Insert(2, "Japan");
combox.Items.Insert(0, "India");
}
XAML
<ComboBox x:Name="combox" SelectionChanged="ComboBox_SelectionChanged_1"/>
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();
你可以像这样使用匿名类:
comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Value";
comboBox.Items.Add(new { Text = "report A", Value = "reportA" });
comboBox.Items.Add(new { Text = "report B", Value = "reportB" });
comboBox.Items.Add(new { Text = "report C", Value = "reportC" });
comboBox.Items.Add(new { Text = "report D", Value = "reportD" });
comboBox.Items.Add(new { Text = "report E", Value = "reportE" });
更新:虽然上面的代码将正确显示在组合框中,你将不能使用组合框的SelectedValue或SelectedText属性。为了能够使用这些,绑定组合框如下:
comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Value";
var items = new[] {
new { Text = "report A", Value = "reportA" },
new { Text = "report B", Value = "reportB" },
new { Text = "report C", Value = "reportC" },
new { Text = "report D", Value = "reportD" },
new { Text = "report E", Value = "reportE" }
};
comboBox.DataSource = items;
您必须创建自己的类类型并重写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());
}