【问题标题】:What is the difference between the HeaderTemplate and ContainerStyle on the WPF DataGrid's GroupStyle?WPF DataGrid 的 GroupStyle 上的 HeaderTemplate 和 ContainerStyle 有什么区别?
【发布时间】:2013-09-25 13:18:54
【问题描述】:

当同时指定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。我实际上并没有直接从那里拿这个例子,但这是一个很棒的网站,所以他们无论如何都可以有一个链接。

【问题讨论】:

    标签: wpf xaml datagrid .net-3.5 groupstyle


    【解决方案1】:

    GroupStyle.HeaderTemplate 属性允许您设置DataTemplate 来定义DataGrid 中的组标题的外观。这是标题通常出现在每组顶部的部分。

    来自MSDN

    获取或设置用于显示组头的模板。

    GroupStyle.ContainerStyle 属性允许您添加一个Style,它定义了每个组项的容器的外观。可以将其想象为每个组项目所在的“盒子”。在这种情况下,盒子内的数据将由 DataTemplate 定义为 DataGrid.ItemsTemplate

    来自MSDN

    使应用程序编写者能够为样式提供自定义选择逻辑,以应用于每个生成的GroupItem

    更新>>>

    响应您的评论...您应该看到两者。我猜你的代码来自 WPF Tutorials.NET 上的 WPF DataGrid Control 文章(你真的应该链接到,除非你想侵犯他们的版权),这是你的问题......他们没有正确地实现了ContainerStyle

    更准确地说,他们没有正确实现ContainerStyle 中的ControlTemplate。当你定义一个ControlTemplate 时,通常习惯在里面添加一个ContentPresenter 来“呈现内容”,在这种情况下来自HeaderTemplate 中的DataTemplate。如果添加一个,您看到两个模板都在工作:

    <ControlTemplate TargetType="{x:Type GroupItem}">
        <Expander IsExpanded="true" Background="Violet">
            <Expander.Header>
                <DockPanel TextBlock.FontWeight="Bold">
                    <ContentPresenter /> 
                </DockPanel>
            </Expander.Header>
            <ItemsPresenter />
        </Expander>
    </ControlTemplate>
    

    试着记住这一点:

    绑定到DataTemplates 中的数据类型属性...线索就在名称中。

    再次定义ControlControlTemplates...中的样子,线索...名称。

    【讨论】:

    • 感谢您的解释,但我仍然不明白为什么您看不到两者?如果我定义了组头的模板和容器模板的样式,为什么在容器模板上方看不到头模板?
    • 答案是默认情况下 GroupItem 的模板具有标题的占位符,该占位符由 HeaderTemplate 填充,但是如果您覆盖 GroupItem 的模板并且不提供标题的占位符那么它不会被使用?
    猜你喜欢
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    相关资源
    最近更新 更多