【发布时间】:2020-07-23 14:30:05
【问题描述】:
我已指定要在 WPF 数据网格中显示的报告列表。
作为报告对象属性的所有值都显示正常,但是报告构造函数中包含的对象的值仅显示数据网格中的通用对象名称,我不确定如何访问任何基础属性从所述对象中为用户提供更有意义的显示。
报表构造函数:
public Report(string reporterName, Asset asset, Location incidentLocation, Room room, string incidentType)
{
ReporterName = reporterName;
Asset = asset;
IncidentLocation = incidentLocation;
Room = room;
IncidentType = incidentType;
}
数据网格 XAML:
<DataGrid Name="ReportsGrid" HorizontalAlignment="Left" Height="212" Margin="33,252,0,0" VerticalAlignment="Top" Width="725"/>
使用报表对象列表设置数据网格:
List<Report> reports = new List<Report>();
reports.Add(report);
ReportsGrid.ItemsSource = reports;
任何帮助或建议将不胜感激。感谢您的宝贵时间。
【问题讨论】:
-
如果你只是给 WPF 一个对象,它会在它上面调用 ToString(),默认情况下,它会给出类名。您可以在 xaml 绑定中显示带有点符号的属性,例如
asset.someproperty而不仅仅是asset。如果要显示整个对象的表示,则覆盖 ToString() 以返回所需的内容。 -
我将在 xaml 绑定的哪个部分配置该属性?我想从资产对象中获取底层资产名称属性来显示。
-
这些列是自动生成的还是您明确定义了 DataGridColumn?
-
目前自动生成,如果它是我想要实现的更可取的实现,我很乐意切换。
标签: c# wpf data-binding datagrid