【问题标题】:GridViewColumnHeader not spanning whole ListView widthGridViewColumnHeader 不跨越整个 ListView 宽度
【发布时间】:2019-04-18 07:42:10
【问题描述】:

我的 WPF 项目中的 ListView / GridView 组合的布局有问题。列标题只是没有跨越父 ListView 的整个宽度。我试图到处查找文档,但没有找到有关底层容器/控件模板的任何线索。

我猜标题的标准控件模板包含一些我不关心的东西,我就是无法理解它。

(简化的)ListView 在包含标题的行的左侧和右侧有几个像素空间,其中父 ListView 的背景是可见的:

https://i.imgur.com/XijV88a.jpg

问题:如何使列标题跨越 ListView 的整个宽度并使少数像素空间消失?

我的 XAML:

<ListView Padding="0"
          BorderThickness="0"
          Background="Red">

    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Background" Value="LightGray" />
            <Setter Property="Margin" Value="0" />
            <Setter Property="Padding" Value="0" />
            <Setter Property="BorderThickness" Value="0" />
        </Style>
    </ListView.ItemContainerStyle>

    <ListView.View>
        <GridView>
            <GridView.ColumnHeaderContainerStyle>
                <Style TargetType="{x:Type GridViewColumnHeader}">
                    <Setter Property="Foreground" Value="White" />
                    <Setter Property="Background" Value="Black" />
                    <Setter Property="BorderThickness" Value="0" />
                    <Setter Property="Margin" Value="0" />
                    <Setter Property="Padding" Value="0" />
                    <Setter Property="HorizontalAlignment" Value="Stretch" />
                </Style>
            </GridView.ColumnHeaderContainerStyle>

            <GridViewColumn Header="1" Width="40"/>
            <GridViewColumn Header="2" Width="40"/>
            <GridViewColumn Header="3" Width="40"/>

        </GridView>
    </ListView.View>

    <ListViewItem> 1 </ListViewItem>
    <ListViewItem> 2 </ListViewItem>
</ListView>

到目前为止,实现我想要的外观的唯一方法是将列表视图的背景更改为与标题相同的颜色 - 不过,必须有更好的方法。任何帮助表示赞赏!

【问题讨论】:

    标签: wpf xaml listview gridview columnheader


    【解决方案1】:

    此列不支持“*”作为宽度参数,因此您不能轻易说 - 填充其余部分...

    你应该做的是连接 ListView 的 SizeChanged 事件并在那里进行计算。因此,例如,在您的情况下,如果我们希望前两列各取 40,第三列取其余部分,您可以这样做:

    private void ListView_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        ListView listView = sender as ListView;
        GridView gView = listView.View as GridView;
    
        var workingWidth = listView.ActualWidth - SystemParameters.VerticalScrollBarWidth;
    
        gView.Columns[0].Width = 40;
        gView.Columns[1].Width = 40;
        gView.Columns[2].Width = workingWidth - 80;
    }
    

    编辑

    对于标题行中的间隙(向左和向右),您希望像这样更改 ColumnHeaderContainerStyle:

    <GridView.ColumnHeaderContainerStyle>
        <Style TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Background" Value="Black" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Margin" Value="-2,0,-2,0" />
            <Setter Property="Padding" Value="0" />
            <Setter Property="HorizontalAlignment" Value="Stretch" />
        </Style>
    </GridView.ColumnHeaderContainerStyle>
    

    因此,请确保将边距每边减少 -2。

    【讨论】:

    • 感谢您的回答,但这不是我的意思。我担心标题行最右边和最左边的空间。有一些像素没有被填充,我不知道为什么。它们在图像中可见,ListView 的红色背景“闪耀”。
    • 非常感谢,这成功了!不确定这些边距从何而来,但现在看起来符合预期。
    • 窥探可视化树是值得的。我怀疑标题行位于某种边框中,其填充为 2,0,2,0。
    猜你喜欢
    • 1970-01-01
    • 2010-10-07
    • 2014-05-12
    • 2021-05-26
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 2011-10-03
    • 2020-11-02
    相关资源
    最近更新 更多