Search code examples
wpfcomboboxobjectdataprovider

Populating ComboBox With Object Property Using ObjectDataProvider


I have an ObjectDataProvider defined as follows:

<ObjectDataProvider x:Key="employeeDataProvider" ObjectType="{x:Type cbb2:EmployeeAccess}" MethodName="getEmployees">            
</ObjectDataProvider>

As you can see the method getEmployees is called which returns a list of type Employee. The Employee class is defined as follows:

class Employee
{
    public string name { get; set; }
    public int id { get; set; }
}

My XAML page has a combo box that I want to populate with the employee name. Here is what I have so far:

<ComboBox ... ItemsSource="{Binding Source={StaticResource employeeDataProvider}}"/>

The problem I am having is the combo box is being populated with the Employee object and not the name property of the Employee.

Here is a picture of what's happening:

combo box

My question is, how to I get the combo box to populate with just the name property of the employee?

Thanks!


Solution

  • add this property to the combo box

    DisplayMemberPath="name"
    

    eg:

    <ComboBox DisplayMemberPath="name" ItemsSource="{Binding Source={StaticResource employeeDataProvider}}"/>