我有一组对象,我想将它们显示为DataGrid
。我的集合中有一个嵌套类型集合的问题
我需要在同一网格中显示它们。请在下面找到我的对象:
public class DataSource
{
public string Name { get; set; }
public List<Block> Children { get; set; }
}
public class Block
{
public string BlockName { get; set; }
public int BlockNumber { get; set; }
public string BlockAdress { get; set; }
public string BlockStatus { get; set; }
}
public ObservableCollection SourceData { get; set; }
我的Xaml
<StackPanel Orientation="Vertical">
<DataGrid Name="DgDataSource" AutoGenerateColumns="False" CanUserAddRows="False" ItemsSource="{Binding SourceData}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="1*"/>
<DataGridTextColumn Header="Block Name" Binding="{Binding BlockName}" Width="1*"/>
<DataGridTextColumn Header="Block Number" Binding="{Binding BlockNumber}" Width="1*"/>
<DataGridTextColumn Header="Block Adress" Binding="{Binding BlockAdress}" Width="1*"/>
<DataGridTextColumn Header="BlockStatus" Binding="{Binding BlockStatus}" Width="1*"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel
>
如何在DataGrid
中显示块列表。请在附图中找到我希望GridControl的行为方式。
答案 0 :(得分:1)
这将对您有所帮助:
<DataGrid AutoGenerateColumns="False" x:Name="DataGrid_Test">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=Name}" VerticalAlignment="Center" Margin="3,3,3,3"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Childrens">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" ItemsSource="{Binding Children}">
<DataGrid.Columns>
<DataGridTextColumn Header="Block Name" Binding="{Binding BlockName}" />
<DataGridTextColumn Header="Block Number" Binding="{Binding BlockNumber}" />
<DataGridTextColumn Header="Block Adress" Binding="{Binding BlockAdress}" />
<DataGridTextColumn Header="BlockStatus" Binding="{Binding BlockStatus}" />
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>