如何使用DataTable绑定ComboBox [英] how to bind ComboBox with DataTable
问题描述
我的DataTable有以下列:
I have the DataTable with following columns:
id,Name,Description,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文件,put:
In your XAML file, put:
<ComboBox x:Name="myComboBox" DisplayMemberPath="Name" SelectedValuePath="id" />
在你的代码背后,放:
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
这篇关于如何使用DataTable绑定ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!