WPF Combobox DisplayMemberPath

comboboxwpf

Ok, I looked at other questions and didn't seem to get my answer so hopefully someone here can.

Very simple question why does the DisplayMemberPath property not bind to the item?

<ComboBox Grid.Row="1" Grid.Column="2" ItemsSource="{Binding PromptList}" DisplayMemberPath="{Binding Name}" SelectedItem="{Binding Prompt}"/>

The trace output shows that it is trying to bind to the class holding the IEnumerable not the actual item in the IEnumerable. I'm confused as to a simple way to fill a combobox without adding a bunch a lines in xaml.

It simply calls the ToString() for the object in itemssource. I have a work around which is this:

<ComboBox Grid.Row="1" Grid.Column="2" ItemsSource="{Binding PromptList}"  SelectedItem="{Binding Prompt}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

But in my opinion it's too much for such a simple task. Can I use a relativesource binding?

Best Answer

DisplayMemberPath specifies the path to the display string property for each item. In your case, you'd set it to "Name", not "{Binding Name}".

Related Topic