wpf - WPF DataGrid 的 GroupStyle 上的 HeaderTemplate 和 ContainerStyle 有什么区别?

标签 wpf xaml datagrid .net-3.5 groupstyle

看起来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适本地。
更准确地说,他们还没有实现 ControlTemplateContainerStyle适本地。当您定义 ControlTemplate 时,一般习惯上加一个ContentPresenter inside 以“呈现内容”,在本例中来自 DataTemplateHeaderTemplate .如果添加一个,您将看到两个模板都在工作:
<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 DataTemplates... the clue is in the name.

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

关于wpf - WPF DataGrid 的 GroupStyle 上的 HeaderTemplate 和 ContainerStyle 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18911907/

相关文章:

c# - 不安全边缘检测仍然很慢

c# - 如何将单元格样式应用于 DataGrid 单元格

c# - WPF 中的动态条件格式

wpf - 如何通过重新定义的列表框模板使用 UI 虚拟化

wpf - 如何在WPF DataGrid中实现可编辑的DataGridComboBoxColumn

wpf - HierarchicalDataTemplate 中的固定项目

xaml - View 模型属性更改时如何更改图像源

c# - 动态地将 BoxView 添加到网格 [Xamarin.Forms]

.net - Windows XP 中的 WPF DataGrid 显示问题

c# - MVVM DataGrid在刷新内容后如何设置焦点