Sean
Sean

Reputation: 133

Silverlight listbox question

I'm using listBox.ItemsSource = e.Result.Persons, which is a collection of persons. The listbox shows the actual object names when I would like it to show the first name of each person object. How can I do this?

Upvotes: 0

Views: 232

Answers (4)

Panagiotis Lefas
Panagiotis Lefas

Reputation: 1155

You can override the ToString() method of the Persons object so that it display the first name of the person.

Upvotes: 0

Eilistraee
Eilistraee

Reputation: 8290

Or use the dedicated "DisplayMemberPath" property, which do exactly what you want easily without any side effects (nor additional markup):

<ListBox DisplayMemberPath="FirstName" />

For more complicated item representations, use templates (see below).

Upvotes: 0

Keith Adler
Keith Adler

Reputation: 21178

In addition to the method of binding specified by the other response, you could simply bind it as follows:

listBox.ItemsSource = e.Result.Persons.Select(d => new { FirstName });

Upvotes: 0

Sonosar
Sonosar

Reputation: 526

use Listboxes ItemTemplate. something like this.

<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FirstName}"/>
</ListBox.ItemTemplate>
</DataTemplate>
</ListBox>

Upvotes: 1

Related Questions