如何使用DataBinding实现ComboBox?

时间:2013-03-12 20:28:01

标签: c# wpf data-binding combobox

我想将以下内容从Windows窗体翻译成WPF:

achievements = DB.FillDataTable(String.Format("SELECT [id], [category], [name], [description], [count_threshold1], [count_threshold2], [count_threshold3], [count_threshold4], [count_threshold5], [date_threshold], [type], [iconImage] FROM [avatar_achievement] WHERE active = 'Y' ORDER BY [name]"));
                DataRow row = achievements.NewRow();
                row["name"] = String.Empty;
                achievements.Rows.InsertAt(row, 0);
                comboBoxAchName.DataSource = achievements;
                comboBoxAchName.DisplayMember = "name";
                comboBoxAchName.ValueMember = "id";

非常简单,这需要从SQL DB中进行一些选择,并将它们放入显示“name”列的comboBox中,并将“id”列存储为值。现在,当用户在下拉列表中进行选择时,他们正在选择“Oranges”但我的代码可以获取值“222”。我需要两条信息。

我如何重写那个确切的代码块,以便在WPF组合框中工作?

1 个答案:

答案 0 :(得分:0)

显然,当我发布这个问题时,我对另一个问题感到厌倦和沮丧......我可以发誓我昨天在谷歌搜索了......无论如何,这个链接给了我答案:

wpf combobox binding