【问题标题】:How to force zero pixel Gap between Consecutive Cells in WPF DataGrid如何在 WPF DataGrid 中的连续单元格之间强制零像素间隙
【发布时间】:2011-12-18 15:10:15
【问题描述】:

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

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

。 请注意两个连续单元格之间的 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. 查阅了一些书籍。

但似乎什么都没有发生。

是否有解决此/一些 hack 或解决方法的方法,或者我必须从头开始创建所有内容?我在 c# 方面有不错的经验,但我是 WPF 新手。

请帮忙。

【问题讨论】:

    标签: wpf datagrid cells gaps-in-visuals


    【解决方案1】:

    您需要做的是获取 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>
    

    你也可以把它放在其他地方,这取决于你的具体需求。

    【讨论】:

    • 哇!那行得通!这为我节省了很多时间!我想不出这个。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多