是否可以使用 Wpf DataGrid 在集合中嵌套集合?

发布于 2024-09-25 03:12:05 字数 47 浏览 4 评论 0原文

我想要一个简单的示例程序,它使用 Wpf DataGrid 将集合嵌套在集合中。

I want a simple sample program that nests collections within collections using Wpf DataGrid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

非要怀念 2024-10-02 03:12:05

下面是使用 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>

替代文本

Here's an implementation using VB.Net codebehind. Code is needed only to create test data.

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

Here's the 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>

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文