Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

wpf - how to bind ComboBox with DataTable

I have the DataTable with following columns:

id, Name, Description, ParentId

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Like so:

In your XAML file, put:

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

In your code behind, put:

myComboBox.ItemsSource = myTable;

(myTable being a reference to the table you mentioned)

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

NameComboBox.SelectedValue

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...