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.
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
DataTemplate
s... the clue is in the name.Define what the
Control
looks like inControlTemplate
s... again, clue... name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With