[英]Set Combobox Item from C# using Value in WPF
I have combobox in my XAML window,I have one datatable which contains two columns ID,Name . 我的XAML窗口中有组合框,我有一个数据表,其中包含两列ID,Name 。 I bind that datable in to combobox items using below code.
我使用以下代码将该数据绑定到组合框项目。
cBox.SelectedValuePath="ID";
cBox.DisplayMemberPath="Name";
cBox.ItemsSource = dtable.DefaultView;
What I want is, I want to select particular item from c# using ID (not Name) , then it will select 1st element of the combobox like below 我想要的是,我想使用ID(不是Name)从c#中选择特定项目,然后它将选择组合框的第一个元素,如下所示
cBox.SelectedIndex = 0/1/2/3/...;
If I use below code, it will select any item in the combobox but I want to select combobox item using ID 如果我使用下面的代码,它将在组合框中选择任何项目,但我想使用ID选择组合框项目
cBox.Text = dtable.Rows[1][1].ToString();
you have to use the selectedValue property.. 您必须使用selectedValue属性。
try the below 试试下面
cBox.SelectedValue = dtable.Rows[1][0].ToString();
you can download the working sample from here.. 您可以从此处下载工作示例。
https://drive.google.com/uc?export=download&id=0Bxxluya0NKB2dGZOMjc0SlRJVWc https://drive.google.com/uc?export=download&id=0Bxxluya0NKB2dGZOMjc0SlRJVWc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.