tags:

views:

939

answers:

3

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?

+4  A: 

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

Ben M
you're absolutely amazing, I can't believe i missed the obvious.
Jose
glad to help! (with some padding to satisfy SO)
Ben M
Same problem here. Thanks you - SO to the rescue again!
MarkB
+1  A: 

you are not binding to the data in the class, you are telling it to get it's data from the class member that is named by the member "name" so, if your instance has item.Name == "steve" it is trying to get the data from item.steve. for this to work, you should remove the binding from the MemberPath. Change it to MemberPath = "Name" this tells it to get the data from the member "Name". that way it will call item.Name, not item.steve.

Muad'Dib
A: 

It is true , you should change the Memberpath="{Bingint Name}" to MemberPath"Name" then it will work

Emu