如何在 WPF 中的列表框中呈现对象集合
我有一系列想要展示的物品。我该怎么做?列表框可以,但每个对象都有许多我想呈现的属性。我已经将一个列表框绑定到一个集合,并且列出了所有对象。我的问题是关于列表框的可视化,以及列表框是否正确使用,或者还有其他我应该使用的东西。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现现有的答案有点缺乏,通常不会处理集合或将其项目简化为字符串,最好的情况是使用
CollectionView
(例如排序、分组),但通常您使用 数据模板显示允许您的各个项目使用它们的所有属性。此外,还有一些与集合配合良好的控件,首先您需要知道是否需要选择,如果不是
ItemsControl
是一个不错的选择,否则你应该使用ListBox
或ListView
。如果您的对象有不同的视图(例如详细信息视图和缩略图视图),通常会使用 ListView。您可以使用
ListView.View< /code>
为此,框架中有一个现有视图,即
GridView
,提供列。 Matthew Ferreira 所建议的正是您不应该对 ListView 执行的操作,因为您希望使模板依赖于当前视图,事实上该代码甚至无法编译,因为DataTemplate
只能有一个子级。ListView 应该将视图逻辑封装在其视图中,以便可以随意更改。如果您决定使用
ItemsControl
或ListBox
,则设置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 aListBox
orListView
.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, theGridView
, 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 sinceDataTemplate
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
orListBox
then setting theItemTemplate
is what you want to do. Read the Data Templating overview i linked to, it makes for a good starting point.您可能需要考虑使用 ListView 控件反而。如果您计划显示对象的多个属性,
ListView
支持列。您可以使用ItemTemplate
属性来格式化对象的显示。例如:此示例假设您的对象具有属性
Title
和ShouldCheck
。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 theItemTemplate
property to format the display of your object. For example:This example assumes that your object has the properties
Title
andShouldCheck
.您的对象集合可能会被视为您的模型。 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.