I'm using listBox.ItemsSource = e.Result.Persons
, which is a collection of persons. The listbox show开发者_JS百科s the actual object names when I would like it to show the first name of each person object. How can I do this?
use Listboxes ItemTemplate. something like this.
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FirstName}"/>
</ListBox.ItemTemplate>
</DataTemplate>
</ListBox>
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 });
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).
You can override the ToString() method of the Persons object so that it display the first name of the person.
精彩评论