我需要创建一个 DataGrid 来显示 WPF 中的对象集合。该集合在运行时出现,并且大多数时候都不同。
对象的属性还可以是一个集合。因此每个单元格本身应该能够显示一个子DataGrid,并且可以扩展到第n 级。
如何在 WPF 中创建这样的 DataGrid?
我需要创建一个 DataGrid 来显示 WPF 中的对象集合。该集合在运行时出现,并且大多数时候都不同。
对象的属性还可以是一个集合。因此每个单元格本身应该能够显示一个子DataGrid,并且可以扩展到第n 级。
如何在 WPF 中创建这样的 DataGrid?
您可以使用分层数据模板来实现此目的。
例如,请参阅下面的 MVVM 模式代码。
模型.cs
public class Person
{
ObservableCollection<Person> MyCollection {get; set;}
}
视图模型.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>
希望对你有帮助.....
If all the data will be dynamic, then I would say the recommended approach would be to create your DataGrid on code-behind, and populate its columns according to your necessities. There is a DataGridTemplateColumn
1 class that I think will help you. You can assign a DataTemplate
to this column via the CellTemplate
property.
You can create that DataTemplate
via XAML or code-behind. And of course that template can contain a DataGrid. You won't be able to databind the DataGridTemplateColumn
itself, but you can databind the elements inside the DataTemplate
.
DataGridTemplateColumn at MSDN
Example
This is a simple example of how to do it in XAML, like I said if you need dynamic datagrids then you have to do it in code-behind. I hope this helps.
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>
您可以使用 datagrid 控件并将其 itemsource 设置为要在每一行中显示的可观察数据集合。并且对于每一行,您还可以根据需要设置其内容模板。此内容模板的控件数据也可以使用绑定来设置。