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

Bha*_*jwa 6 wpf datagrid cells gaps-in-visuals

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

与数据网格的问题是,它强制最小的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>
Run Code Online (Sandbox Code Playgroud)

我到目前为止尝试过:

  1. 关闭GridLine(参见结果:http://img263.imageshack.us/img263/8602/figure2j.png)
  2. 将content属性的边距设置为0.
  3. 搜索谷歌和stackoverflow
  4. 咨询了一些书.

但似乎没有出现.

是否有解决方案/某些黑客或解决方法或者我是否必须从头开始创建所有内容?我对c#有很好的经验,但我是WPF的新手.

请帮忙.

Mat*_*ton 6

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

<Style TargetType="DataGridCell">
    <Setter Property="BorderThickness" Value="0" />
</Style>
Run Code Online (Sandbox Code Playgroud)

我建议将它放入DataGrid的资源中,这样它只影响那个网格,除非你希望它具有更广泛的范围.

<DataGrid>
    <DataGrid.Resources>
        <Style TargetType="DataGridCell">...</Style>
    </DataGrid.Resources>
    ...
</DataGrid>
Run Code Online (Sandbox Code Playgroud)

根据您的确切需求,您还可以使用其他地方.