简体   繁体   English

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

[英]What is the difference between the HeaderTemplate and ContainerStyle on the WPF DataGrid's GroupStyle?

It would appear that the ContainerStyle is used in preference to the HeaderTemplate when 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 HeaderTemplate does 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 . 编辑链接到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. GroupStyle.HeaderTemplate属性允许您设置DataTemplate以定义DataGrid的组标题的外观。 This is the part where the title generally appears at the top of each group. 这是标题通常出现在每个组顶部的部分。

From MSDN : 来自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. GroupStyle.ContainerStyle属性允许您添加一个Style ,用于定义每个组项的容器的外观。 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 . 可以把它想象成每个组项目所在的“框”。在这种情况下,框中的数据看起来像是由设置为DataGrid.ItemsTemplateDataTemplate定义的。

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 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. 我猜你的代码来自WPF Tutorials.NET上的WPF DataGrid Control文章(除非你想侵犯他们的版权,否则你真的应该链接到这个)这就是你的问题...他们还没有正确实现ContainerStyle

To be more accurate, they have not implemented the ControlTemplate in the ContainerStyle properly. 为了更准确,他们没有正确地在ContainerStyle实现ControlTemplate 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 . 定义ControlTemplate ,通常会在内部添加ContentPresenter来“呈现内容”,在这种情况下,它来自HeaderTemplateDataTemplate 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. 绑定到DataTemplate的数据类型属性...线索在名称中。

Define what the Control looks like in ControlTemplate s... again, clue... name. ControlTemplate定义Control样子...再次,clue ... name。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM