【问题标题】:Hide HeaderContent from the top of a ListView从 ListView 的顶部隐藏 HeaderContent
【发布时间】:2017-05-22 11:47:28
【问题描述】:

我有一个

<DockPanel>
    <!-- Header -->
    <Button DockPanel.Dock="Top" Command="{Binding CreateAccountCommand}" Margin="{StaticResource ControlMargin}" Style="{StaticResource IconButtonStyle}">
        <StackPanel>
            <MahAppsIconPacks:PackIconFontAwesome Kind="Pencil" />
            <TextBlock>create account</TextBlock>
        </StackPanel>
    </Button>

    <!-- Accounts list -->
    <ListView SelectionMode="Single" SelectedIndex="0" ItemsSource="{Binding AccountViewModels}" SelectedItem="{Binding SelectedItem}" Style="{StaticResource AccountsList}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <LocalViews:AccountView Margin="{StaticResource ControlMargin}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ListView>
</DockPanel>

它的顶部带有黑色分隔符,非常难看。我该如何隐藏它?我试过设置不同的样式,但是没有ListViewColumnHeader

在实时树中,它显示为标题内容......不是分隔符,我的错。如何删除它?

这不起作用:

<Style TargetType="ListView" x:Key="AccountsList" BasedOn="{StaticResource {x:Type ListView}}">
        <Style.Resources>
            <Style TargetType="HeaderedContentControl">
                <Setter Property="Visibility" Value="Collapsed" />
            </Style>
        </Style.Resources>
    </Style>

【问题讨论】:

  • 您指的是 WPF 应用程序上的调试工具栏 Visual Studio 叠加层吗?如果是这样,您可以根据以下问题停用它:stackoverflow.com/questions/36618494/…
  • 不,不是。稍后我会添加截图。

标签: wpf xaml mahapps.metro


【解决方案1】:

您是否使用了ListView 的任何查看功能? (例如,我认为是默认的 GridView 等。在这种情况下,分隔符可能是标题部分,但为空)。如果不是,请尝试只使用ListBox。它默认没有标题。

编辑:我刚刚检查了这个理论,但似乎我根本无法重现您的问题。这是一个 ListBox,因为您已经获得了您的代码(以及一个很好的 ListView):

<Window.Resources>
    <x:Array x:Key="Items" Type="sys:String">
        <sys:String>Item 1</sys:String>
        <sys:String>Item 2</sys:String>
        <sys:String>Item 3</sys:String>
    </x:Array>
</Window.Resources>
<StackPanel Margin="20">
    <TextBlock Text="ListBox:"/>
    <ListBox Height="100" ItemsSource="{StaticResource Items}" BorderBrush="Transparent">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ListBox>
    <TextBlock Text="ListView:"/>
    <ListView Height="100" ItemsSource="{StaticResource Items}" BorderBrush="Transparent">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ListView>
</StackPanel>

这可能是 Windows 7 上的不同主题,但我使用的是 Windows 10。

【讨论】:

  • 不,不使用ListView.View
  • 我已经用我的完整代码和截图更新了这个问题。
  • 您尝试过使用列表框吗?正如另一个答案所提到的,这与 listView 标头的样式有关。 Header 是您未使用的 ListView 的一项功能,因此您不妨使用 ListBox。
  • 是的,就是这样。如果您将其发布为答案,我将接受它。但是,它引入了另一个问题,即滚动,但这是一个不同的主题。
  • 就是上面的答案!很高兴它修复了它。我总是发现需要几次尝试才能让滚动在这些环境中起作用,尤其是水平方向。
【解决方案2】:

我相信这是 MahApps 模板的结果。这不是标准 WPF ListView 的默认行为。

您必须调整 MahApps 模板才能摆脱它。 像这样的:

<Style TargetType="{x:Type ListView}" BasedOn="{StaticResource {x:Type ListView}}">
    <Setter Property="BorderThickness" Value="0" />
</Style>

MahApps 代码和资源都是开源的。我认为您遇到的特定问题是由 BorderThickness 设置器驱动的:

https://github.com/MahApps/MahApps.Metro/blob/develop/src/MahApps.Metro/MahApps.Metro/Styles/Controls.ListView.xaml

 <Style x:Key="MetroListView" TargetType="{x:Type ListView}">
    <Setter Property="OverridesDefaultStyle" Value="true" />
    <Setter Property="AlternationCount" Value="2" />
    <Setter Property="Background" Value="{DynamicResource WhiteBrush}" />
    <Setter Property="BorderBrush" Value="{DynamicResource BlackBrush}" />
    <Setter Property="BorderThickness" Value="0 1 0 0" />

【讨论】:

    猜你喜欢
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多