Reputation: 13161
I need to create a DataGrid to display collection of objects in WPF. The collection comes at run-time and is different most of the times.
The properties of the object can further be a collection.So each cell should be capable of displaying a sub-DataGrid in itself, and this can extend upto nth level.
How to create such a DataGrid in WPF ?
Upvotes: 3
Views: 2797
Reputation: 1028
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>
Upvotes: 1
Reputation: 39
You can use Hierarchical Data Template to achieve this.
For Example refer the below MVVM pattern code.
Model.cs
public class Person
{
ObservableCollection<Person> MyCollection {get; set;}
}
ViewModel.cs
public class PersonModel
{
ObservableCollection Collection {get; set;}
}
XAML Code(View)
<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>
Hope this will help you.....
Upvotes: 2
Reputation: 9242
you can use datagrid control and can set its itemsource to a observable collection of data that you want to display in each row. and for each row you can also set its content template according to your need . this content template's controls's data can also be set using binding.
Upvotes: 1