WPF DataGrid 的 GroupStyle 上的 HeaderTemplate 和 ContainerStyle 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18911907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between the HeaderTemplate and ContainerStyle on the WPF DataGrid's GroupStyle?
提问by Julius
It would appear that the ContainerStyleis used in preference to the HeaderTemplatewhen both are specified, as below;
当两者都被指定时,似乎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>
Is the only difference that the HeaderTemplatedoes not have access to the ItemsPresenter, or is the difference something to do with hierarchical data structures?
唯一的区别是HeaderTemplate无法访问ItemsPresenter,还是与分层数据结构有关?
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.
编辑链接到http://wpftutorial.net/DataGrid.html#grouping。我实际上并没有直接从那里拿这个例子,但它是一个很棒的网站,所以他们无论如何都可以有一个链接。
回答by Sheridan
The GroupStyle.HeaderTemplateproperty lets you set a DataTemplateto define what the group headers in the DataGridwill look like. This is the part where the title generally appears at the top of each group.
该GroupStyle.HeaderTemplate属性允许您设置 aDataTemplate以定义DataGrid遗嘱中的组标题。这是标题通常出现在每个组顶部的部分。
From MSDN:
从MSDN:
Gets or sets the template that is used to display the group header.
获取或设置用于显示组标题的模板。
The GroupStyle.ContainerStyleproperty lets you add a Stylethat 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 DataTemplateset as the DataGrid.ItemsTemplate.
该GroupStyle.ContainerStyle属性允许您添加一个Style定义每个组项目的容器的外观。将此DataTemplate视为每个组项目所在的“盒子”。在这种情况下,盒子内的数据的外观由集合定义为DataGrid.ItemsTemplate.
From MSDN:
从MSDN:
Enables the application writer to provide custom selection logic for a style to apply to each generated
GroupItem.
使应用程序编写者能够为样式提供自定义选择逻辑以应用于每个生成的
GroupItem.
UPDATE >>>
更新 >>>
In response to your comment... you should see both. I'm guessing that your code comes from the WPF DataGrid Controlarticle on WPF Tutorials.NET (which you reallyshould have linked to unless you want to infringe their copyright) and this is your problem... they have notimplemented the ContainerStyleproperly.
为了回应您的评论……您应该同时看到两者。我猜你的代码来自WPF Tutorials.NET 上的WPF DataGrid Control文章(你真的应该链接到,除非你想侵犯他们的版权),这是你的问题......他们没有ContainerStyle正确实现。
To be more accurate, they have not implemented the ControlTemplatein the ContainerStyleproperly. When you define a ControlTemplate, it is generally customary to add a ContentPresenterinside to 'present the content', which in this case comes from the DataTemplatein the HeaderTemplate. If you add one, you willsee both the templates working:
为了更准确,他们还没有实现ControlTemplate在ContainerStyle正确。当您定义ControlTemplate,通常的惯例是为增加ContentPresenter内部“存在的内容”,在这种情况下,来自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>
Try to remember this:
试着记住这一点:
Bind to your data type properties in
DataTemplates... the clue is in the name.Define what the
Controllooks like inControlTemplates... again, clue... name.
绑定到
DataTemplates 中的数据类型属性... 线索就在名称中。再次定义s... 中的
Control外观ControlTemplate,提示...名称。

