What is the difference between the HeaderTemplate and ContainerStyle on the WPF DataGrid's GroupStyle?
Asked Answered
S

1

8

It would appear that the ContainerStyle is used in preference to the HeaderTemplate when both are specified, as below;

<controls:DataGrid.GroupStyle>
  <GroupStyle>
    <GroupStyle.HeaderTemplate>
      <DataTemplate>
        <StackPanel>
          <TextBlock Text="{Binding Path=Name}" Background="Yellow" />
        </StackPanel>
      </DataTemplate>
    </GroupStyle.HeaderTemplate>
    <GroupStyle.ContainerStyle>
      <Style TargetType="{x:Type GroupItem}">
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
              <Expander IsExpanded="true" Background="Violet">
                <Expander.Header>
                  <DockPanel TextBlock.FontWeight="Bold">
                    <TextBlock Text="{Binding Path=Name}" />
                    <TextBlock Text="{Binding Path=ItemCount}"/>
                  </DockPanel>
                </Expander.Header>
                <ItemsPresenter />
              </Expander>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </GroupStyle.ContainerStyle>
  </GroupStyle>
</controls:DataGrid.GroupStyle>

Is the only difference that the HeaderTemplate does not have access to the ItemsPresenter, or is the difference something to do with hierarchical data structures?

Thanks!

Edited to link to http://wpftutorial.net/DataGrid.html#grouping. I didn't actually take the example directly from there, but it's a great site so they can have a link anyway.

Saretta answered 20/9, 2013 at 8:15 Comment(0)
D
9

The GroupStyle.HeaderTemplate property lets you set a DataTemplate to define what the group headers in the DataGrid will look like. This is the part where the title generally appears at the top of each group.

From MSDN:

Gets or sets the template that is used to display the group header.

The GroupStyle.ContainerStyle property lets you add a Style that defines what the container of each group item will look like. Think of this like the 'box' that each group items will sit in. In this case, what the data inside the box will look like is defined by a DataTemplate set as the DataGrid.ItemsTemplate.

From MSDN:

Enables the application writer to provide custom selection logic for a style to apply to each generated GroupItem.

UPDATE >>>

In response to your comment... you should see both. I'm guessing that your code comes from the WPF DataGrid Control article on WPF Tutorials.NET (which you really should have linked to unless you want to infringe their copyright) and this is your problem... they have not implemented the ContainerStyle properly.

To be more accurate, they have not implemented the ControlTemplate in the ContainerStyle properly. When you define a ControlTemplate, it is generally customary to add a ContentPresenter inside to 'present the content', which in this case comes from the DataTemplate in the HeaderTemplate. If you add one, you will see both the templates working:

<ControlTemplate TargetType="{x:Type GroupItem}">
    <Expander IsExpanded="true" Background="Violet">
        <Expander.Header>
            <DockPanel TextBlock.FontWeight="Bold">
                <ContentPresenter /> 
            </DockPanel>
        </Expander.Header>
        <ItemsPresenter />
    </Expander>
</ControlTemplate>

Try to remember this:

Bind to your data type properties in DataTemplates... the clue is in the name.

Define what the Control looks like in ControlTemplates... again, clue... name.

Delenadeleon answered 20/9, 2013 at 8:24 Comment(2)
Thanks for the explanation, but I still don't understand why you don't see both? If I define how a template for the header of the group and a style that templates the container, why would I not see the header template above the container template?Saretta
Is the answer that by default the template for a GroupItem has a placeholder for the header, which is populated by the HeaderTemplate, but if you overwrite the GroupItem's template and don't provide a placeholder for the header then it will not be used?Saretta

© 2022 - 2024 — McMap. All rights reserved.