Silverlight列表框问题

发布于 2024-10-26 11:39:54 字数 109 浏览 10 评论 0原文

我正在使用 listBox.ItemsSource = e.Result.Persons,它是人员的集合。当我希望它显示每个人对象的名字时,列表框显示实际的对象名称。我该怎么做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尽揽少女心 2024-11-02 11:39:54

使用列表框 ItemTemplate。
像这样的东西。

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

use Listboxes ItemTemplate.
something like this.

<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FirstName}"/>
</ListBox.ItemTemplate>
</DataTemplate>
</ListBox>
温暖的光 2024-11-02 11:39:54

除了其他响应指定的绑定方法之外,您还可以简单地绑定它,如下所示:

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

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 });
简单气质女生网名 2024-11-02 11:39:54

或者使用专用的 “DisplayMemberPath”属性,它可以轻松地完成您想要的操作,没有任何副作用(也没有额外的标记):

<ListBox DisplayMemberPath="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).

我早已燃尽 2024-11-02 11:39:54

您可以重写 Persons 对象的 ToString() 方法,以便它显示人员的名字。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文