Bayern
Bayern

Reputation: 350

How to fil a ComboBox with MVVM Light binding and XAML?

I have a list that with items that I want to show in a ComboBox, but the result is that I don't see the text but this:

 App1.Data.Models.Test
 App1.Data.Models.Test
 App1.Data.Models.Test

I don't really know how to show the proper text.

The model Test has 2 properties ID and Name.

 <ComboBox Grid.Column="1"
           Grid.Row="3"
           Margin="10"
           ItemsSource="{Binding TestList}" />

Do I need to use DataTemplate like a ListView?

Upvotes: 0

Views: 499

Answers (2)

Gabriel Negut
Gabriel Negut

Reputation: 13960

The ComboBox uses the ToString() method of its items to display them.

Either override App1.Data.Models.Test.ToString(), or choose a specific property of App1.Data.Models.Test to display, let's say Name:

<ComboBox
       Grid.Column="1"
       Grid.Row="3"
       Margin="10"
       ItemsSource="{Binding TestList}"
       DisplayMemberPath="Name" />

Upvotes: 2

Dom84
Dom84

Reputation: 864

You need to define the Property fromApp1.Data.Models.Test you want to display by DisplayMemberPath. Alternatively override ToString()

<ComboBox Grid.Column="1"
       Grid.Row="3"
       Margin="10"
       DisplayMemberPath = "Name"
       ItemsSource="{Binding TestList}" />

Upvotes: 1

Related Questions