摆脱xaml中GridViewColumnHeader末尾的白线

发布于 2024-12-21 20:37:03 字数 1457 浏览 1 评论 0原文

到目前为止,我在 GridViewColumnHeaders 方面遇到了几个问题。最初,我遇到了一个问题,即每个列标题之间都有一小片白色。即使我们将 borderthickness 设置为 0,白线仍然存在。环顾四周后,我发现我必须使用 ControlTemplate 将标题更改为默认具有文本框属性。我使用了这段代码:

<Style x:Key="gridHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                        <TextBox Text="{TemplateBinding Content}" 
                                 FontWeight="Bold"
                                 FontFamily="Arial"
                                 FontSize="11"
                                 Foreground="#00648D"
                                 Padding="5,0,5,0" 
                                 BorderBrush="#7EB0CC" 
                                 BorderThickness="0,0,2,2"
                                 HorizontalContentAlignment="Center"
                                 VerticalContentAlignment="Center"
                                 IsReadOnly="True"
                                 Background="Transparent"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

这有效并删除了标题列之间的白色小条,它还阻止用户移动和调整列的大小,这会弄乱格式,所以这很好。但是,在 gridviewcolumnheader 的最末端仍然有一点白色,如下图所示:

在此处输入图像描述

有没有办法删除它?

I've had several problems with the GridViewColumnHeaders so far. Originally I had a issue with there being a tiny sliver of white between each of the column headers. Even if we set the borderthickness to 0, the white lines would still exist. After looking around, I found that I had to use a ControlTemplate to change the header to default to having textbox attributes. I used this code:

<Style x:Key="gridHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                        <TextBox Text="{TemplateBinding Content}" 
                                 FontWeight="Bold"
                                 FontFamily="Arial"
                                 FontSize="11"
                                 Foreground="#00648D"
                                 Padding="5,0,5,0" 
                                 BorderBrush="#7EB0CC" 
                                 BorderThickness="0,0,2,2"
                                 HorizontalContentAlignment="Center"
                                 VerticalContentAlignment="Center"
                                 IsReadOnly="True"
                                 Background="Transparent"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

This worked and removed the little sliver of white between the header columns and it also prevented the user from moving and resizing the column which messed up the formatting so that was good. However there is still a little sliver of white at the very end of the gridviewcolumnheader as shown in the image below:

enter image description here

Is there a way to remove that too?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素染倾城色 2024-12-28 20:37:03

该空白是您抓取并调整标题大小的对象。我相信即使您重写标题模板它也存在,因为它是 GridView 模板的一部分,而不是标题列

我不确定是否有办法覆盖这些颜色而不覆盖整个 GridView 模板,但是您可以导航加载视觉树后,从那里手动设置背景颜色。

以下是使用 ListView 的 Loaded 事件和一些 可视化树助手

private void ListView_Loaded(object sender, RoutedEventArgs e)
{
    var thumb = VisualTreeHelpers.FindChild<Thumb>((DependencyObject)sender, "PART_HeaderGripper");
    if (thumb == null) return;
    thumb.Background = Brushes.Transparent;

    var thumbContent = VisualTreeHelpers.FindChild<Border>(thumb);
    if (thumbContent == null) return;
    thumbContent.Background = Brushes.Transparent;
}

结果

Example

如果您想覆盖 ListView 的 ControlTemplate,可以找到默认的 XAML 这里

That whitespace is the object you grab and resize the Header with. I believe it exists even if you re-write the header template because it is part of the GridView template, not the header column

I'm not sure if there's a way to overwrite those colors without overwriting the entire GridView template, however you can navigate the Visual Tree once it's loaded and manually set the background color from there.

Here's an example using the Loaded event of the ListView and some Visual Tree Helpers

private void ListView_Loaded(object sender, RoutedEventArgs e)
{
    var thumb = VisualTreeHelpers.FindChild<Thumb>((DependencyObject)sender, "PART_HeaderGripper");
    if (thumb == null) return;
    thumb.Background = Brushes.Transparent;

    var thumbContent = VisualTreeHelpers.FindChild<Border>(thumb);
    if (thumbContent == null) return;
    thumbContent.Background = Brushes.Transparent;
}

Result

Example

If you would rather to overwrite the ListView's ControlTemplate, you can find the default XAML here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文