WPF Datagrid grouping column header question

May 29, 2009 at 2:27 AM

 

I have grouped my data in a datagrid and used a customized expander to hide the toggle button. I have expanded it to the right. The result is shown below.

My question is how to access/set the value of the column header above group names?

Can someone please help on this, none of the things I have tried out helped.

 

Jun 3, 2009 at 11:54 PM

Here are some more details on the issue.

I have grouped my data in a datagrid but instead of the common way to use an expander, I have just used  a stackpanel with horizontal orientation.

The result is shown below.

item # 1. I am trying to set the name of the group (country in this example) above the area where the name of the countries appear.
I tried various permutations and combinations but I am not able to set the name or the width of that location.  And a major layout issue is that the header is not aligned with the body, the state/city/name headers do not appear above their columns.

item # 3. I am not sure why there is a gap between the country name ( group header ) and the rest of the datagrid.

I am including my code below for reference.

<Window x:Class="WPFSample.Window3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Grouping" Height="547" Width="375"
    xmlns:local="clr-namespace:WPFSample"
    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase">

    <Window.Resources>
        <ObjectDataProvider x:Key="ppl"
            ObjectType="{x:Type local:ppl}"
            MethodName="getResult"/>
        <CollectionViewSource x:Key="cvs" Source="{Binding Source={StaticResource ppl}}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Country" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Window.Resources>

    <dg:DataGrid Background="White" Name="ChecksGrid"
                     HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled"
                     AutoGenerateColumns="False" AlternationCount="2"
                     ItemsSource="{Binding Source={StaticResource cvs}}"
                     BorderThickness="0" SelectionMode="Single"
                     SelectionUnit="FullRow" GridLinesVisibility="All"
                     VerticalGridLinesBrush="DarkGray">
        <dg:DataGrid.GroupStyle>
            <GroupStyle>
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <dg:DataGridRowsPresenter/>
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock FontWeight="Bold" Text="{Binding Path=Name}"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <StackPanel Orientation="Horizontal">
                                        <Border BorderBrush="Black" BorderThickness="1,1,0,0.5">
                                        <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
                                            <ContentPresenter Content="{Binding Path=Name}" Width="35"/>
                                        </Grid>
                                        </Border>
                                        <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
                                            <ItemsPresenter/>
                                        </Grid>
                                    </StackPanel>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </dg:DataGrid.GroupStyle>
        <dg:DataGrid.Columns>
            <dg:DataGridTextColumn Binding="{Binding Path=State, Mode=TwoWay}" Header="State" />
            <dg:DataGridTextColumn Binding="{Binding Path=City, Mode=TwoWay}" Header="City" />
            <dg:DataGridTextColumn Binding="{Binding Path=FirstName, Mode=TwoWay}" Header="First Name" />
            <dg:DataGridTextColumn Binding="{Binding Path=LastName, Mode=TwoWay}" Header="Last Name" />
        </dg:DataGrid.Columns>
    </dg:DataGrid>
</Window>

Jul 20, 2009 at 2:49 PM

Hi ,

Even im also facing same issue.Whenever i group contents ,alignment is not maintained.Any clues on this .

 

With regards,

Mahender

Nov 25, 2009 at 11:14 AM

Hi,

Here is an usefull link:

http://sweux.com/blogs/smoura/index.php/wpf/2009/04/27/wpf-toolkit-datagrid-part-ii-custom-styling/

I hope that this doesn't come to late.

Regards,

John.