95

I want to be able to hide the header at the top of each grid column in a WPF ListView.

This is the XAML for my ListView:

   <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>

The data I am binding this to is:

 <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>
Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
Ozplc
  • 1,091
  • 2
  • 10
  • 15

3 Answers3

156

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>
Ray
  • 45,695
  • 27
  • 126
  • 169
  • 2
    Shouldn't this be FrameworkElement.Visibility, at least in WPF 3.5? (I know this is an old topic, but it's at the top on Google, so it's useful for a lot of people) – Roy T. Aug 11 '11 at 11:00
  • 1
    It doesn't need to be. You've specified the target type, so you don't need to specify where the property is. – Ray Aug 11 '11 at 11:05
  • Excellent post...saved me a heap of time and worked a treat .. thanks – Kev Dec 19 '12 at 20:54
  • 1
    If reuse isn't a factor, this can be done inline on the GridView too: `` – HotN Jan 04 '19 at 23:40
67

You can also put the Style inline like so (but see improvement below):

<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.

Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
23

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...

Darren
  • 4,408
  • 4
  • 39
  • 57