我想要一个简单的示例程序,使用Wpf DataGrid将集合嵌套在集合中。
答案 0 :(得分:2)
这是使用VB.Net代码隐藏的实现。仅需要代码来创建测试数据。
Class MainWindow
Public Property cs As New List(Of c1)
Sub New()
' This call is required by the designer.
InitializeComponent()
For i1 = 1 To 3
Dim c1 = New c1
cs.Add(c1)
c1.c1text = i1
For i2 = 1 To 3
Dim c2 = New c2
c1.c1col.Add(c2)
c2.c2text = i1 & i2
For i3 = 1 To 3
Dim c3 = New c3
c2.c2col.Add(c3)
c3.c3text = i1 & i2 & i3
For i4 = 1 To 3
Dim c4 = New c4
c3.c3col.Add(c4)
c4.c4text = i1 & i2 & i3 & i4
For i5 = 1 To 3
c4.c4col.Add(i1 & i2 & i3 & i4 & i5)
Next
Next
Next
Next
Next
DataGrid1.ItemsSource = cs
End Sub
End Class
Public Class c1
Public Property c1text As String
Public Property c1col As New List(Of c2)
End Class
Public Class c2
Public Property c2text As String
Public Property c2col As New List(Of c3)
End Class
Public Class c3
Public Property c3text As String
Public Property c3col As New List(Of c4)
End Class
Public Class c4
Public Property c4text As String
Public Property c4col As New List(Of String)
End Class
这是Xaml:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid Name="DataGrid1" IsReadOnly="True" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding c1text}" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid Name="DataGrid2" ItemsSource="{Binding c1col}" IsReadOnly="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding c2text}" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid Name="DataGrid3" ItemsSource="{Binding c2col}" IsReadOnly="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding c3text}" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid Name="DataGrid4" ItemsSource="{Binding c3col}" IsReadOnly="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding c4text}" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid Name="DataGrid5" ItemsSource="{Binding c4col}" IsReadOnly="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding}" />
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>