如何删除 WPF GridView 中自动添加的列 (HeaderRole=Padding)

发布于 2024-10-09 10:04:35 字数 549 浏览 4 评论 0 原文

我正在尝试在 WPF 中制作自定义 gridview 但我刚刚意识到链接 说:

GridViewHeaderRowPresenter 类 执行列标题的布局 在 GridView 中并放置一个附加的 列标题末尾添加空格

,并且由于末尾有附加列标题,因此最后一列的右侧有一个额外的列。所以我想知道是否可以修改它,请参考下面的图片 image

我的问题是:

  1. 我是否可以从网格中删除“A”部分?
  2. 无论如何,我可以将“B”部分(角色=填充标题)的相同效果添加到“C”部分(第一列之前)吗?

谢谢

I'm trying to make a custom gridview in WPF but I just realized that as this link says:

The GridViewHeaderRowPresenter class
performs layout for the column headers
in a GridView and places an additional
column header at the end to add space

and because of the additional column header at the end, there's an extra column to the right of the last column. so I was wondering if I can modify it, please refer to this image below image

My questions are:

  1. Is there anyway I can remove the 'A' part from the grid?
  2. Is there anyway I can add the same effect of 'B' part (the role = padding header) to the 'C' Part (before the first column)?

Thanks

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

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

发布评论

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

评论(2

独守阴晴ぅ圆缺 2024-10-16 10:04:35

经过一番摸索后,我设法通过在几个地方设置负边距来隐藏这个额外的列。这虽然很老套,但很有效:

<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="Margin" Value="0,0,-7,1"/>
</Style>

 <Style TargetType="{x:Type GridViewColumnHeader}">
    <Setter Property="Margin" Value="0,0,-2,0"/>
</Style>

也许还可以添加

<Trigger Property="Role" Value="Padding">
    <Setter Property="Visibility" Value="Collapsed"/>
</Trigger>

GridViewColumnHeader 样式。

第一个似乎可以根据需要设置为负值;当它不能再减少时,它就会达到上限。这样做仍然会在标题的右侧留下一个 2px 的间隙,这是第二种样式修复的。

唉,-2 边距适用于所有 GridViewColumnHeaders(并且样式触发器方法似乎不起作用),因此它可能会稍微扰乱内部边框和内容对齐,但如果可以忍受,则效果很好。

编辑:实际上是的,划伤-2边距,它有太多负面影响。

After poking around a lot I managed to hide this extra column just by setting negative margin in a few places. It's quite hacky but it works:

<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="Margin" Value="0,0,-7,1"/>
</Style>

 <Style TargetType="{x:Type GridViewColumnHeader}">
    <Setter Property="Margin" Value="0,0,-2,0"/>
</Style>

And perhaps add

<Trigger Property="Role" Value="Padding">
    <Setter Property="Visibility" Value="Collapsed"/>
</Trigger>

in the GridViewColumnHeader style, as well.

First one can be set as negative as you like, it seems; it caps when it can't reduce anymore. Doing so will still leave a wee 2px gap on the right side of the headers, which the 2nd Style fixes.

Alas, the -2 margin applies to all GridViewColumnHeaders (and the Style Trigger method doesn't seem to work), so it can mess with inner borders and content alignment a bit, but if that's tolerable this works fine.

EDIT: Actually yeah scratch the -2 margin, it has too many negative side effects.

Inside <Style x:Key="{x:Static GridView.GridViewScrollViewerStyleKey}" TargetType="{x:Type ScrollViewer}"> you'll find a <GridViewHeaderRowPresenter>; set its Margin equal to "0,0,-2,0" and you'll get the same effect without the drawbacks.

随心而道 2024-10-16 10:04:35

要删除第一列“C”,您可以将以下属性添加到 DataGrid 定义中:

RowHeaderWidth="0"

To get rid of the first column "C" you can add the following property to your DataGrid definition:

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