如何将 ComboBox 与 DataTable 绑定 [英] how to bind ComboBox with DataTable

查看:22
本文介绍了如何将 ComboBox 与 DataTable 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含以下列的数据表:

I have the DataTable with following columns:

id、名称、描述、ParentId

id, Name, Description, ParentId

并且想要创建一个 WPF 控件(.NET 4.0 框架),它实现了一个组合框,该组合框显示绑定到 id 值的名称.因此,当用户选择组合框中显示的名称时,背后的逻辑必须检索其 id 值.

and would like to create a WPF control (.NET 4.0 framework) which implements a combobox which displays the names which are bound to values of id. So when the user selects a name displayed in the combobox the behind logic has to retrieve its id value.

如果有人能展示如何执行上述操作,我将不胜感激.

I would be very thankful if anyone could show the way of doing the described above.

推荐答案

像这样:

在您的 XAML 文件中,输入:

In your XAML file, put:

 <ComboBox x:Name="myComboBox" DisplayMemberPath="Name" SelectedValuePath="id" />

在你后面的代码中,输入:

In your code behind, put:

myComboBox.ItemsSource = myTable;

(myTable 是对您提到的表格的引用)

(myTable being a reference to the table you mentioned)

然后您可以使用以下表达式到达组合框中当前选定人员的 id:

Then you can reach the id of the currently selected person in the combo box using the expression:

NameComboBox.SelectedValue

这篇关于如何将 ComboBox 与 DataTable 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆