Reputation: 133
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
Reputation: 1155
You can override the ToString() method of the Persons object so that it display the first name of the person.
Upvotes: 0
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
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
Reputation: 526
use Listboxes ItemTemplate. something like this.
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FirstName}"/>
</ListBox.ItemTemplate>
</DataTemplate>
</ListBox>
Upvotes: 1