【问题标题】:DataGrid having DataGrid for a cell upto nth level in WPFDataGrid 具有用于 WPF 中第 n 级单元格的 DataGrid
【发布时间】:2013-06-24 18:44:24
【问题描述】:

我需要创建一个 DataGrid 来显示 WPF 中的对象集合。 该集合在运行时出现,并且大多数时候都不同。

对象的属性还可以是一个集合。因此每个单元格本身应该能够显示一个子DataGrid,并且可以扩展到第n级。

如何在 WPF 中创建这样的 DataGrid ?

【问题讨论】:

    标签: c# .net wpf xaml wpfdatagrid


    【解决方案1】:

    您可以使用 datagrid 控件并将其 itemsource 设置为您希望在每一行中显示的可观察数据集合。并且对于每一行,您还可以根据需要设置其内容模板。该内容模板的控件数据也可以通过绑定来设置。

    【讨论】:

    • 数据将是动态的。每次我们都可以有不同的对象集合。
    【解决方案2】:

    您可以使用分层数据模板来实现这一点。

    例如,参考下面的 MVVM 模式代码。

    模型.cs

    public class Person
    {
        ObservableCollection<Person> MyCollection {get; set;}
    }
    

    ViewModel.cs

    public class PersonModel
    {
        ObservableCollection Collection {get; set;}
    }
    

    XAML 代码(查看)

    <Window.DataContext>
        <local:PersonModel/>
    </Window.DataContext>
    <Grid>
        <DataGrid  ItemsSource="{Binding Collection}" >
            <DataGrid.RowDetailsTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding SelectedItem.MyCollection}">
                    <DataGrid RowDetailsTemplate="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}},Path=RowDetailsTemplate}" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}},Path=SelectedItem.MyCollection}">                   
                    </DataGrid>
                </HierarchicalDataTemplate>
            </DataGrid.RowDetailsTemplate>
        </DataGrid>
    </Grid>
    

    希望对你有所帮助.....

    【讨论】:

    • PersonModel 包含 ObservableCollection 类型的 Collection 属性?
    • 我试过你的解决方案,但它只显示了一个属性(列表类型)的子数据网格。子数据网格中可以显示多个属性。
    • 对于多个 List 类型的属性,上述解决方案将不起作用。
    【解决方案3】:

    如果所有数据都是动态的,那么我会说推荐的方法是在代码隐藏上创建您的 DataGrid,并根据您的需要填充其列。我认为有一个DataGridTemplateColumn1 课程会对您有所帮助。您可以通过CellTemplate 属性为该列分配DataTemplate

    您可以通过 XAML 或代码隐藏创建 DataTemplate。当然,该模板可以包含一个 DataGrid。您将无法对 DataGridTemplateColumn 本身进行数据绑定,但您可以对 DataTemplate 中的元素进行数据绑定。

    DataGridTemplateColumn at MSDN

    示例

    这是一个如何在 XAML 中执行此操作的简单示例,就像我说的,如果您需要动态数据网格,那么您必须在代码隐藏中执行此操作。我希望这会有所帮助。

    MainWindow.xaml

    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="TestStackoverflow.MainWindow"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid>
                <Grid.Resources>
                    <!--DataTemplate for Published Date column defined in Grid.Resources.  PublishDate is a property on the ItemsSource of type DateTime -->
                    <DataTemplate x:Key="DateTemplate" >
                        <DataGrid AutoGenerateColumns="False" >
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="Second grid column" Binding="{Binding ''}" ClipboardContentBinding="{x:Null}"/>
                            </DataGrid.Columns>
                            <System:String>I heard you like</System:String>
                            <System:String>datagrids so</System:String>
                            <System:String>I put a datagrid in</System:String>
                            <System:String>your data datagrid</System:String>
                            <System:String>so you can grid while you grid.</System:String>
                        </DataGrid>
                    </DataTemplate>
                </Grid.Resources>
                <DataGrid AutoGenerateColumns="False" >
                    <DataGrid.Columns>
                        <DataGridTemplateColumn Header="Original Datagrid DG Column" CellTemplate="{StaticResource DateTemplate}" />
                        <DataGridTextColumn Header="Original Datagrid Text Column" Binding="{Binding ''}" ClipboardContentBinding="{x:Null}"/>
                    </DataGrid.Columns>
                    <System:String>String 1</System:String>
                    <System:String>String 2</System:String>
                    <System:String>String 3</System:String>
                    <System:String>String 4</System:String>
                    <System:String>String 5</System:String>
                </DataGrid>
            </Grid>
        </Grid>
    </Window>
    

    【讨论】:

    • 我尝试了全代码方法。没有成功。你有参考吗?
    • @Brij 我添加了一个示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 2013-06-16
    • 1970-01-01
    相关资源
    最近更新 更多