[英]Create a sub-row/child row in datagrid from a list of objects in my parent object C# WPF
So let's say for this example I have an object of type car right. 因此,对于这个示例,我们有一个类型为car right的对象。 The car class is as follows: 车类如下:
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public List<Wheel> Wheels { get; set; }
}
and the Wheel class is as follows: 而Wheel类如下:
class Wheel
{
public int ID { get; set; }
public string Size { get; set; }
public string Shape { get; set; }
public string Brand{ get; set; }
}
so basically what I want is for the List<Wheel> wheels
to display in a sub row/child row 所以基本上我想要的是将List<Wheel> wheels
显示在子行/子行中
See screenshot below: 请参见下面的屏幕截图:
I am working in C# WPF 我在C#WPF中工作
Here is how you can display your data in nested grid way 这是您如何以嵌套网格方式显示数据的方法
<DataGrid ItemsSource="{Binding Path=Cars}" AutoGenerateColumns="false"
RowDetailsVisibilityMode="Visible"
CanUserAddRows="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Make" Binding="{Binding Make}"/>
<DataGridTextColumn Header="Model" Binding="{Binding Model}"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding Path=Wheels}" AutoGenerateColumns="false" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Path=ID}"/>
<DataGridTextColumn Header="Size" Binding="{Binding Path=Size}" IsReadOnly="True"/>
<DataGridTextColumn Header="Shape" Binding="{Binding Path=Shape}" IsReadOnly="True"/>
<DataGridTextColumn Header="Brand" Binding="{Binding Path=Brand}" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.