Combine column header like in Excel

Feb 12, 2009 at 12:21 PM

Hy all.

I have a question about the DataGrid column headers. Would it be possible to combine 2-3 column headers into a new header like in Excel (see image attached)? Something like a bigger header over the normal header? I have seen a sample in which some labels were arraged before the grid....But when a column of the grid it's resized the label will not change. Is there a solution for this?

Thanks a lot.

Nico

Account Report
Personal data Account data
First name Last name Account nr Account sum

Feb 13, 2009 at 2:54 AM
Of course you can.
you can customize the HeaderTemplate .
like the following code  : 

                <WPFToolKits:DataGridTemplateColumn>
                    <WPFToolKits:DataGridTemplateColumn.HeaderTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="Payee"></TextBlock>
                                <StackPanel Orientation="Horizontal" Margin="0,5,0,0">
                                    <TextBlock Text="Category" MinWidth="60"></TextBlock>
                                    <TextBlock Text="Type" Margin="10,0,0,0" MinWidth="60"></TextBlock>
                                    <TextBlock Text="Memo" Margin="10,0,0,0" MinWidth="60"></TextBlock>
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
                    </WPFToolKits:DataGridTemplateColumn.HeaderTemplate>
                    <WPFToolKits:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding Path=PayeeName}"></TextBlock>
                                <StackPanel Orientation="Horizontal" Margin="0,5,0,0">
                                    <TextBlock  MinWidth="60" Text="{Binding Path=Category}"></TextBlock>
                                    <TextBlock  MinWidth="60" Text="{Binding Path=Memo}"></TextBlock>
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
                    </WPFToolKits:DataGridTemplateColumn.CellTemplate>
                </WPFToolKits:DataGridTemplateColumn>
Feb 13, 2009 at 7:49 AM
Thank you very much for the replay.

Yes, this would be a solution, but I have problems with resizing and sorting for those columns.
Is there a way to be able also to sort and resize?

Thanks,
Nico
Feb 18, 2009 at 2:08 PM
Edited Feb 18, 2009 at 2:12 PM

I have tried to use something like this:

<dg:DataGridTemplateColumn>
 
<dg:DataGridTemplateColumn.HeaderTemplate>

<DataTemplate>

<StackPanel> 

<dg:DataGridColumnHeader Content="Title" IsHitTestVisible="False" MouseEnter="DataGridColumnHeader_MouseEnter" MouseLeave="DataGridColumnHeader_MouseEnter"/>
 
<StackPanel Orientation="Horizontal">

<dg:DataGridColumnHeader Width="200" Content="Title" IsHitTestVisible="False"/>
 
<dg:DataGridColumnHeader Width="100" Content="Running" IsHitTestVisible="False"/>

</StackPanel>

</StackPanel>

</DataTemplate>

</dg:DataGridTemplateColumn.HeaderTemplate> 

<dg:DataGridTemplateColumn.CellTemplate>

<DataTemplate>
 
<StackPanel>

<StackPanel Orientation="Horizontal">

<TextBlock Width="200" Text="{Binding Path=Title}"></TextBlock>

<TextBlock Width="100" Text="{Binding Path=Running}"></TextBlock>

</StackPanel>

</StackPanel>

</DataTemplate>

</dg:DataGridTemplateColumn.CellTemplate>  

</dg:DataGridTemplateColumn>

The problem is that if I don't set the IsHitTestVisible="False" property of the DataGridColumnHeader I get an error "Object reference not set to an instance of an object" every time I MouseOver the column header. Does some know why? Can I do something about this?

Thanks.
Nico