如何隐藏 WPF ListView 的标题?

我希望能够隐藏标题在每个网格列的顶部在 WPF 列表视图。

这是我的 ListView 的 XAML:

   <Window x:Class="ListViewTest.Test0.ListViewTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Empty ListView Grid" Height="216" Width="435" FlowDirection="LeftToRight" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.IsSharedSizeScope="False">
<Window.Resources>
<XmlDataProvider x:Key="CustomersDS" Source="C:\data.xml"/>
</Window.Resources>




<ListView Margin="0,0,0,50" ItemTemplate="{DynamicResource CustomerTemplate}" ItemsSource="{Binding Source={StaticResource CustomersDS}, XPath=/Customers/Customer}">
<ListView.View>
<GridView>
<GridViewColumn  DisplayMemberBinding="{Binding XPath=Code}"/>
<GridViewColumn  DisplayMemberBinding="{Binding XPath=Name}"/>
<GridViewColumn  DisplayMemberBinding="{Binding XPath=Country}"/>
</GridView>
</ListView.View>
</ListView>




</Window>

我要绑定的数据是:

 <Customers>
<Customer>
<Code>1234</Code>
<Name>EPI</Name>
<Country>Sesame Street</Country>
</Customer>
<Customer>
<Code>3234</Code>
<Name>Paul</Name>
<Country>United Kingdom</Country>
</Customer>
<Customer>
<Code>3344</Code>
<Name>Juan</Name>
<Country>Spain</Country>
</Customer>
<Customer>
<Code>4321</Code>
<Name>Dodo</Name>
<Country>Mars</Country>
</Customer>
</Customers>
63424 次浏览

Define a Style like so

<Window.Resources>
....
<Style x:Key="myHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Visibility" Value="Collapsed" />
</Style>
</Window.Resources>

Apply it like so

<GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
....
</GridView>

You can also put the Style inline like so:

<ListView>
<ListView.Resources>
<Style TargetType="GridViewColumnHeader">
<Setter Property="Visibility" Value="Collapsed" />
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<!-- ... -->
</GridView>
</ListView.View>
</ListView>

This ensures that the style will only be applied to the desired control (i.e., without unintentionally affecting any additional controls that may be within the XAML scope).

[edit: 2022] A better solution (not mentioned elsewhere on this page) is to entirely disable the header, rather than collapse it. If you never plan to "uncollapse" (show) the header, there's no reason the WPF layout engine should have to consider it at all:

<ListView>
<ListView.Resources>
<Style TargetType="GridViewColumnHeader">
<Setter Property="Template" Value="{x:Null}" />  <!-- way better. -->
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<!-- ... -->
</GridView>
</ListView.View>
</ListView>

The only difference from above is to set the control Template of the sub-scoped GridViewColumnHeader instances to {x:Null}. This allows WPF to avoid preparing for header instance(s) that are never going to be rendered anyway.

Another way you can apply Ray's solution is like this:

<ListView>
<ListView.View>
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="GridViewColumnHeader">
<Setter Property="Visibility" Value="Collapsed" />
</Style>
</GridView.ColumnHeaderContainerStyle>
</GridView>
</ListView.View>
</ListView>

The solution sets the style property directly rather than creating a resource that is automatically applied. Not saying it's better, just another way...