简体   繁体   English

如何获取WPF组合框选择的值(键)

[英]how to fetch wpf combobox selected value (key)

I want to find selected value of wpf combobox here is my code: 我想找到wpf组合框的选定值,这是我的代码:

DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add("ks34dsd45g6g74g", "Amisha");
dt.Rows.Add("2ks34dsd45g6g74g", "Rhitik");
dt.Rows.Add("ks34dsd45g6g74g3", "Aishwarya");
dt.Rows.Add("4ks34ds545g6g74g", "Sonam");
dt.Rows.Add("ks55dsd45g6g74g5", "Aamana");

ddl_BusinessIDNames.DataContext = dt;
ddl_BusinessIDNames.DisplayMemberPath = dt.Columns[1].ToString();
ddl_BusinessIDNames.SelectedValuePath = dt.Columns[0].ToString();

that means at run time combo box show the readable text to user ie Amisha,Rhitik,Aishwarya etc but my question is if user select "sonam" how to fetch the key of "sonam" ie "4ks34ds545g6g74g" because Convert.ToString(ddl_BusinessIDNames.SelectedValue) this code fetch the display text of combo box but what if i want to fetch the key of value "sonam" 这意味着在运行时组合框向用户显示可读文本,即Amisha,Rhitik,Aishwarya等,但是我的问题是用户是否选择“ sonam”,如何获取“ sonam”的密钥,即“ 4ks34ds545g6g74g”,因为Convert.ToString(ddl_BusinessIDNames.SelectedValue)此代码获取组合框的显示文本,但是如果我想获取值“ sonam”的键该怎么办

Please suggest... 请建议...

private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
    label1.Content = dt.Rows[Combobox1.SelectedIndex][0];
}

Combobox1.SelectedIndex - represents selected row index of your datatable Combobox1.SelectedIndex代表数据表的选定行索引

0 - column index in datatable with id, U want to get. 0要获取ID为ID的数据表中的列索引。

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

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