Search code examples
c#wpfxamldata-bindingbinding

How, when you bind data to a collection, do you set it to a property?


Say what I have is an ObservableCollection<People> ListOfPeople such that:

public class People
{
    public string Name { get; set; };
}

I have a combobox written equivilent to:

<ComboBox x:Name="combobox_Profiles" Text="Select Person"
          ItemsSource="{Binding Path=???}">

How can I set the items within the combobox to be the value of each Name property? I thought I knew how to do this, but I clearly do not.


Solution

  • To select what to show in the case of a single property use the DisplayMemberPath and set it to Name.

    You can also set the SelectedValuePath if you want the control to return a specific property of the object rather than the object itself.