看起来ContainerStyle
优先于 HeaderTemplate
使用当两者都被指定时,如下;
<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>
唯一的区别是
HeaderTemplate
无权访问 ItemsPresenter
,还是区别与分层数据结构有关?谢谢!
编辑链接到 http://wpftutorial.net/DataGrid.html#grouping .我实际上并没有直接从那里拿这个例子,但它是一个很棒的网站,所以他们无论如何都可以有一个链接。
最佳答案
GroupStyle.HeaderTemplate
属性允许您设置 DataTemplate
定义 DataGrid
中的组标题看起来像。这是标题通常出现在每个组顶部的部分。
来自 MSDN :
Gets or sets the template that is used to display the group header.
GroupStyle.ContainerStyle
属性允许您添加 Style
它定义了每个组项目的容器的外观。将此视为每个组项目所在的“盒子”。在这种情况下,盒子内的数据的外观由 DataTemplate
定义。设置为 DataGrid.ItemsTemplate
.来自 MSDN :
Enables the application writer to provide custom selection logic for a style to apply to each generated
GroupItem
.
更新 >>>
为了回应您的评论……您应该同时看到两者。我猜你的代码来自 WPF DataGrid Control关于 WPF Tutorials.NET 的文章(除非你想侵犯他们的版权,否则你真的应该链接到它)这是你的问题......他们没有实现
ContainerStyle
适本地。更准确地说,他们还没有实现
ControlTemplate
在 ContainerStyle
适本地。当您定义 ControlTemplate
时,一般习惯上加一个ContentPresenter
inside 以“呈现内容”,在本例中来自 DataTemplate
在 HeaderTemplate
.如果添加一个,您将看到两个模板都在工作:<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="true" Background="Violet">
<Expander.Header>
<DockPanel TextBlock.FontWeight="Bold">
<ContentPresenter />
</DockPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
试着记住这一点: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.
关于wpf - WPF DataGrid 的 GroupStyle 上的 HeaderTemplate 和 ContainerStyle 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18911907/