如何隐藏WPF ListView的标题?


85

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

这是我的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>

Answers:


139

像这样定义样式

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

像这样应用

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

2
至少在WPF 3.5中,这不应该是FrameworkElement.Visibility吗?(我知道这是一个古老的话题,但是它在Google上排在首位,因此对很多人都有用)
Roy T.

1
不必如此。您已经指定了目标类型,因此无需指定属性的位置。

很棒的帖子...为我节省了很多时间并请客..谢谢
Kev

如果重用不是一个因素,也可以在GridView上内联完成:<GridView><GridView.ColumnHeaderContainerStyle><Style TargetType="{x:Type GridViewColumnHeader}"><Setter Property="Visibility" Value="Collapsed" /></Style></GridView.ColumnHeaderContainerStyle></GridView>
HotN

55

感谢您的解决方案。您也可以Style像这样放置内联:

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

(此外,{x:Type}似乎不需要使用您使用的符号)


尽管两个答案都是正确的,但这是最可取的。谢谢。
新蜜蜂

20

应用雷氏解决方案的另一种方法是这样的:

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

该解决方案直接设置样式属性,而不是创建自动应用的资源。不是说更好,而是另一种方式...

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.