Is there any way we can have functionality like DataTextField and DataValueField in WPF ComboBox.
I have this query:
Select UID, EmployeeName from tblSystemEmployee;
I want to display EmployeeName in the combo box but it keeps displaying UID. Is there any way I can achieve it? Thank you for your help?
This is how I am populating my dataset:
Try
cbEmp.Items.Clear()
Dim QueryString As String = "SELECT UID, EmployeeName FROM tblSystemEmployee"
Dim drow As DataRow
drow = Nothing
dsEmp = New DataSet
dsEmp = GetResult(QueryString, True)
If IsValidDataset(dsEmp) Then
For Each drow In dsEmp.Tables(0).Rows
cbEmp.Items.Add(drow(0).ToString())
Next
End If
You need the
DisplayMemberPathproperty for the displayand the
SelectedValuePathproperty for the valueThen you can access to whole selected object (e.g. UID, EmployeeName pair) with the
SelectedItemproperty but if you just want the UID then theSelectedValueproperty is what you need.You can learn more about the workings of the properties at MSDN:
For populating the ComboBox with values you need to set the ItemsSource property with a list:
Instead of
cbEmp.Items.Clear()usecbEmp.ItemsSource = NothingandAnd
A good intro article about WPF databinding and comboboxes.