开发者

Silverlight listbox question

开发者 https://www.devze.com 2023-02-19 19:17 出处:网络
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 o

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消