Datagrid Grouping ColumnHeader?

Nov 14, 2008 at 9:53 AM
Hi,
how can i get the ColumnHeader Text for the grouped column in xaml? here is my groupingstyle:

<GroupStyle>
                        <GroupStyle.Panel>
                            <ItemsPanelTemplate>
                                <Primitives:DataGridRowsPresenter />
                            </ItemsPanelTemplate>
                        </GroupStyle.Panel>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Padding="3"/>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <Expander IsExpanded="False">
                                                <Expander.Header>
                                                    <DockPanel TextBlock.FontWeight="Bold">
<!--HERE I WANT THE COLUMNHEADER TEXT, but dont know how :) -->
                                                        <TextBlock Name="txt1" Text="{Binding Path=Name}" Margin="5,0,5,0" />
                                                        <TextBlock Name="txt3" Text="{Binding Path=ItemCount}"/>
                                                    </DockPanel>
                                                </Expander.Header>
                                                <ItemsPresenter />
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>

Thx Frank
Nov 14, 2008 at 12:55 PM
Hi,

I am assuming that you are setting a GroupDescription on your bound CollectionView in order to group on the column you desire? Unfortunately I cannot see a way to navigate from the Expander, which is bound to the CollectionViewGroup instances (this is where Name and ItemCount come from) to the bound collection of the DataGrid. You could however provide the value in code behind. Try the following:


<Expander IsExpanded="False"  Loaded="Expander_Loaded">
   <Expander.Header>
     <DockPanel TextBlock.FontWeight="Bold">
       <TextBlock Name="HeaderText" />
       <TextBlock Name="txt1" Text="{Binding Path=Name}" Margin="5,0,5,0" />
       <TextBlock Name="txt3" Text="{Binding Path=ItemCount}"/>
     </DockPanel>
   </Expander.Header>
   <ItemsPresenter />
  </Expander>

And in code behind:

private void Expander_Loaded(object sender, RoutedEventArgs e)
{
    Expander expander = sender as Expander;
    TextBlock textBlock = expander.FindName("HeaderText") as TextBlock;
    textBlock.Text = "THE COLUMNHEADER TEXT";
}

Hope that helps.
Colin E.

Nov 18, 2008 at 5:32 AM
Hi Colin,
this works absolut fine for me, thanks a lot!

Frank