WPF - 数据网格中对象的数据模板
我无法找到有关该主题的任何内容。假设我有一个汽车类型列表。每个汽车对象都有一系列属性(即品牌、型号、状况、价格、所有者等)。我想在 DataGrid 中显示汽车列表,但我想控制要显示的属性(例如,我可能不想在列表中显示车主姓名,或者我可能想根据汽车的价格)。
我该如何创建数据模板来实现这一目标(只需要一个基本示例)?
I'm having trouble finding anything on the subject. Let's say I have a list of type Car. Each car object has a range of properties (i.e. make, model, condition, price, owner, etc). I want to display the list of Cars in a DataGrid but I want control over which properties to display (for example, I may not want to display the owner name in the list or I may want to color the row of a car based on the price of the car).
How can I go about creating a data template to achieve this (just a basic example is needed)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了显示和隐藏 DataGrid 列,您需要将每列的可见性绑定到一个布尔属性,该属性确定是否显示该列。关于行背景颜色,您可以添加一个 DataGrid 行样式,使用将汽车价格转换为相关行颜色画笔的值转换器将行背景属性绑定到汽车价格。请参阅以下概念证明:
In order to show and hide the DataGrid columns, you need to bind the visibility of each column to a Boolean property that determines whether to show this column or not. regarding the row background color you can add a DataGrid row style that binds the row Background property to the car price using a value converter that converts the car price to the relevant row color brush. See the following proof of concept: