如何在 WPF DataGrid 中强制连续单元格之间的像素间隙为零

发布于 2024-12-12 17:06:32 字数 1453 浏览 2 评论 0原文

我正在 WPF 中构建一个应用程序以满足自定义需求。首先我选择了自定义控件,但后来发现我需要的大部分内容已经在 Datagrid Control 中实现了。然而,有一个小问题:

Datagrid 的问题在于它强制两个连续单元格之间的最小间隙为2 像素间隙(网格线每侧各 1 个)。为了清楚起见,请看下图:

。 请注意 Datagrid 在两个连续单元格之间强制执行的 2 个像素间隙: http://img265.imageshack.us/img265/3545/figurem.png

(堆栈溢出不允许我将图像添加到我的问题中,引用新用户的垃圾邮件保护策略)

这不符合我的要求,因为我希望内容显示为“连续”(不得有 2 个像素的间隙;我希望连接线看起来“连接”)。我尝试过调整 GridLinesVisibility,但没有帮助。 DataGrid正在托管这样的自定义控件:

<DataGrid.Columns>
            <DataGridTemplateColumn Width="25" Header="">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ContentControl Content="{Binding Path=MyCustomControl}" Margin="0"></ContentControl>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

....
</DataGrid.Columns>

到目前为止我已经尝试过:

  1. 关闭GridLine(请参阅此处的结果:http://img263.imageshack.us/img263/8602/figure2j.png)
  2. 将内容属性的边距设置为 0。
  3. 搜索了google和stackoverflow
  4. 查阅了一些书籍。

但似乎什么也没发生。

是否有针对此/某些黑客的解决方案或解决方法,或者我是否必须从头开始创建所有内容?我对 C# 有很好的经验,但我对 WPF 很陌生。

请帮忙。

i am building an application in WPF for a custom need. First i opted for custom controls, but later on figured out that much of what i needed was already implemented in Datagrid Control. However there is one small glitch:

The problem with Datagrid is that it enforces a minimum of 2 pixel gap between two consecutive cells (1 on each side of the Grid Line). Please have a look at the following diagram for clarity:

.
Please note the 2 pixels gap enforced by the Datagrid between the two consecutive cells:
http://img265.imageshack.us/img265/3545/figurem.png

(Stack overflow wouldn't let me add image to my question citing a spam protection policy for new users)

.

This doesn't suit my requirement as i want the content to appear "continuous" (There must not be this gap of 2 pixels; i want the connecting lines to look "connected"). i've tried fidgeting with the GridLinesVisibility, but it didn't help. The DataGrid is hosting a custom control like this:

<DataGrid.Columns>
            <DataGridTemplateColumn Width="25" Header="">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ContentControl Content="{Binding Path=MyCustomControl}" Margin="0"></ContentControl>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

....
</DataGrid.Columns>

i have so far tried:

  1. Switching off the GridLine (see result here: http://img263.imageshack.us/img263/8602/figure2j.png)
  2. Setting the margin of the content property to 0.
  3. Searched google and stackoverflow
  4. Consulted some books.

but nothing seems to come up.

Is there a solution to this/some hack or a workaround or would i have to create everything from scratch? i have decent experience with c#, but i am new to WPF.

Please Help.

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

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

发布评论

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

评论(1

不一样的天空 2024-12-19 17:06:32

您需要做的是获取 DataGrid 的 DataGridCell 样式并将其 BorderThickness 设置为 0。它在默认样式中硬编码为 1,但幸运的是很容易覆盖它:

<Style TargetType="DataGridCell">
    <Setter Property="BorderThickness" Value="0" />
</Style>

我建议将其放入以下资源中您的 DataGrid 因此它只会影响该一个网格,除非您希望它具有比该网格更广泛的范围。

<DataGrid>
    <DataGrid.Resources>
        <Style TargetType="DataGridCell">...</Style>
    </DataGrid.Resources>
    ...
</DataGrid>

您也可以将其放在其他地方,具体取决于您的具体需求。

What you need to do is get at the DataGridCell style for your DataGrid and set its BorderThickness to be 0. It's hard-coded as 1 in the default style, but fortunately it's easy to override this:

<Style TargetType="DataGridCell">
    <Setter Property="BorderThickness" Value="0" />
</Style>

I would suggest putting that into the resources of your DataGrid so it only affects that one grid, unless you want it to have a wider scope than that.

<DataGrid>
    <DataGrid.Resources>
        <Style TargetType="DataGridCell">...</Style>
    </DataGrid.Resources>
    ...
</DataGrid>

There are other places you could put it as well, depending on your precise needs.

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