如何在 WPF 中的列表框中呈现对象集合

发布于 2024-11-09 01:44:59 字数 118 浏览 0 评论 0 原文

我有一系列想要展示的物品。我该怎么做?列表框可以,但每个对象都有许多我想呈现的属性。我已经将一个列表框绑定到一个集合,并且列出了所有对象。我的问题是关于列表框的可视化,以及列表框是否正确使用,或者还有其他我应该使用的东西。

I have a collection of objects that I want to present. How can I do this? A listbox would do but each object has many attributes which I want to present. I already bound a listbox to a collection and I have all my objects listed. My question is regarding the visualization of the listbox and if the listbox is the correct thing to use or there is something else that I should use.

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

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

发布评论

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

评论(3

与风相奔跑 2024-11-16 01:44:59

我发现现有的答案有点缺乏,通常不会处理集合或将其项目简化为字符串,最好的情况是使用 CollectionView(例如排序、分组),但通常您使用 数据模板显示允许您的各个项目使用它们的所有属性。

此外,还有一些与集合配合良好的控件,首先您需要知道是否需要选择,如果不是 ItemsControl 是一个不错的选择,否则你应该使用 ListBoxListView

如果您的对象有不同的视图(例如详细信息视图和缩略图视图),通常会使用 ListView。您可以使用 ListView.View< /code> 为此,框架中有一个现有视图,即 GridView,提供列。 Matthew Ferreira 所建议的正是您不应该对 ListView 执行的操作,因为您希望使模板依赖于当前视图,事实上该代码甚至无法编译,因为 DataTemplate 只能有一个子级。

ListView 应该将视图逻辑封装在其视图中,以便可以随意更改。如果您决定使用 ItemsControlListBox,则设置 ItemTemplate 是您想要做的。阅读我链接到的数据模板概述,它是一个很好的起点。

I find the existing answers to be a bit lacking, normally one would not process collections or boil their items down to a string, at the very best you would do some dynamic manipulation using a CollectionView (e.g. sorting, grouping), but normally you use Data Templating to display the individual items which allows you to use all their properties.

Further there are several controls which work well with collections, firstly you need to know if you want selection, if not an ItemsControl is a good choice, otherwise you should use a ListBox or ListView.

ListViews are normally employed if you have different views for your objects, e.g. a details view and a thumbnail view. You can use the ListView.View for this, there is one existing view in the framework, the GridView, which offers columns. What Matthew Ferreira suggested is exactly what you should not do with a ListView since you want to make the templates dependent on the current view, in fact that code does not even compile since DataTemplate can only have one child.

ListViews are supposed to encapsulate the view logic in their view so it can be changed at will. If you decide to use a ItemsControl or ListBox then setting the ItemTemplate is what you want to do. Read the Data Templating overview i linked to, it makes for a good starting point.

2024-11-16 01:44:59

您可能需要考虑使用 ListView 控件反而。如果您计划显示对象的多个属性,ListView 支持列。您可以使用 ItemTemplate 属性来格式化对象的显示。例如:

<ListView ItemsSource="{Binding Path=myObjectCollection}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Title}"/>
            <CheckBox IsChecked="{Binding Path=ShouldCheck}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

此示例假设您的对象具有属性 TitleShouldCheck

You might want to consider using a ListView control instead. ListView has support for columns if you are planning on showing several properties from your object. You can use the ItemTemplate property to format the display of your object. For example:

<ListView ItemsSource="{Binding Path=myObjectCollection}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Title}"/>
            <CheckBox IsChecked="{Binding Path=ShouldCheck}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

This example assumes that your object has the properties Title and ShouldCheck.

雪花飘飘的天空 2024-11-16 01:44:59

您的对象集合可能会被视为您的模型。 WPF 中通常的做法是添加一个 ViewModel,它将模型数据转换并公开为适合绑定的形式。根据您想要执行的操作,您的 VM 可以将每个对象格式化为字符串表示形式,然后将其公开为列表框可以绑定并显示的字符串集合。

Your collection of object is probably to be viewed as your model. The usual thing in WPF is to add a ViewModel that translates and exposes the model data into a form suitable for binding. Depending on what you want to do, your VM could e.g. format each object into a string representation and then expose it as a collection of strings that the Listbox can bind to and display.

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