问题
How do I pass the categoryId for the selected CategoryName in the first combo box, which would populate the products in the second combo box /
the table structure is a below.
Table_Category CategoryId CategoryyName
Table_Product ProductId ProductName CategoryId ProductPrice ProductUnit
Thanks, Hussain Patel
回答1:
for the first combo box (cmbcategory) using ADO.NET (code), get category object (containing the categoryId and categoryName) and bind to itemsource property of first combo box (cmbcategory)
Example cmbcategory.ItemsSource = categories.DefaultView;
In the xaml code for the cmbcategory add two attributes DisplayMemberPath and SelectedValuePath. assign the the "CategoryName" and "CategoryID" to them. Example
Now you can retrieve the category id for the item selected in the cmbcategory,as below string tmp = cmbcategory.SelectedValue.ToString();
This can be passed to the ADO.NET SQL command object and products can be retrieved and the bindded to the cmbproduct combo box as below
cmbproduct.ItemsSource = products.DefaultView;
Thus for each category selected in the first cmbcategory the products are populated in the cmbproducts.
Thanks, hussain
来源:https://stackoverflow.com/questions/22495422/passing-id-from-one-combobox-to-populate-second-combo-box-in-wpf