如何去除 ListView GroupStyle 的页眉分隔线?

8
在Windows通用平台上的ListViews中,当您使用ListView分组时,组标题下方会出现一个难看的分隔线。即使是微软自己的应用程序中也删除了该行。在Live Visual Tree中,您可以看到它是GridViewHeaderItem中的矩形。我已经阅读过可以将其高度设置为0的内容。我该如何做呢?
以下是设置标题项文本的代码:
<ListView.GroupStyle>
  <GroupStyle HidesIfEmpty="True" >
     <GroupStyle.HeaderTemplate>
        <DataTemplate>
                  <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
     </GroupStyle.HeaderTemplate>
   </GroupStyle>
</ListView.GroupStyle>
1个回答

22

在generic.xaml文件中可以找到所有的默认样式,路径为

C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10240.0\Generic

深入研究该文件,您也可以找到ListViewHeaderItem的默认样式:

<!-- Default style for Windows.UI.Xaml.Controls.ListViewHeaderItem -->
<Style TargetType="ListViewHeaderItem">
    <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
    <Setter Property="FontSize" Value="{ThemeResource ListViewHeaderItemThemeFontSize}" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Margin" Value="0,0,0,4"/>
    <Setter Property="Padding" Value="12,8,12,0"/>
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="VerticalContentAlignment" Value="Top" />
    <Setter Property="MinHeight" Value="{ThemeResource ListViewHeaderItemMinHeight}"/>
    <Setter Property="UseSystemFocusVisuals" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewHeaderItem">
                <StackPanel Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                    <ContentPresenter x:Name="ContentPresenter"
                                      Margin="{TemplateBinding Padding}"
                                      Content="{TemplateBinding Content}"
                                      ContentTemplate="{TemplateBinding ContentTemplate}"
                                      ContentTransitions="{TemplateBinding ContentTransitions}"
                                      HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    <Rectangle Stroke="{ThemeResource SystemControlForegroundBaseLowBrush}"
                               StrokeThickness="0.5"
                               Height="1"
                               VerticalAlignment="Bottom"
                               HorizontalAlignment="Stretch"
                               Margin="12,8,12,0"/>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

只需将该样式复制到您项目的ResourceDictionary中,并删除有问题的Rectangle(因此仅剩下ContentPresenter作为子元素)即可。

<Rectangle Stroke="{ThemeResource SystemControlForegroundBaseLowBrush}"
           StrokeThickness="0.5"
           Height="1"
           VerticalAlignment="Bottom"
           HorizontalAlignment="Stretch"
           Margin="12,8,12,0"/>

有没有一种方法可以在不覆盖默认样式的情况下完成这个操作?我想要将其放置在一个控件上。 - Shahar Prish
你可以创建一个自定义控件,并将修改后的样式设置为该控件的默认样式。你可以将 SystemControlForegroundBaseLowBrush 更改为页面的背景色(尽管这可能会产生副作用)。真正的问题在于默认的 StrokeThickness 是一个固定值而不是绑定值。 - Bart
1
对于GridView也非常有效。 - 27k1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接