如何隐藏listview WPF的头?

时间:2021-07-07 19:43:21

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

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

This is the XAML for my 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>

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>

3 个解决方案

#1


120  

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>

#2


41  

Thanks for this solution. 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>

(Also, the {x:Type} notation you used doesn't seem to be needed)

(同样,您使用的{x:Type}表示法似乎不需要)

#3


10  

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

另一种应用Ray解的方法是:

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

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

#1


120  

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>

#2


41  

Thanks for this solution. 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>

(Also, the {x:Type} notation you used doesn't seem to be needed)

(同样,您使用的{x:Type}表示法似乎不需要)

#3


10  

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

另一种应用Ray解的方法是:

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

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