如何在一像素宽度中设置网格线(WPF DataGrid)?

4

我正在使用WPF数据网格来显示一些数据,我希望网格线的厚度等于一个像素,但是每个单元格都显示了不必要的边框。如何摆脱这个问题并将所有线条的厚度设置为一个像素(就像在WinForms中使用DataGridView一样)?

XAML:
<DataGrid HeadersVisibility="Column" SelectionUnit="Cell">
   <DataGrid.Columns>
      <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>                                    
      <DataGridTextColumn Header="Description" Binding="{Binding Description}"/>
   </DataGrid.Columns>
</DataGrid>

结果: 不必要的边框


可能是与操作系统有关,我在Win10上尝试了样例,没有这样的阴影。 - Shakra
3个回答

3
这就是解决方案!设置参数RenderOptions.EdgeMode="Aliased"。非常感谢David Kossoglyad提供的解决方案。
<DataGrid RenderOptions.EdgeMode="Aliased" UseLayoutRounding="True" ....>

1
这可能对你有帮助。
<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
        <Setter Property="BorderThickness" Value="1,0,0,1"/>
        <Setter Property="BorderBrush" Value="Black"/>
    </Style>
</DataGrid.CellStyle>

当我将属性GridLinesVisibility设置为“None”值时,所有网格线都被删除了。 我需要网格线,但是宽度只有一个像素。 - kostey2204
问题并没有得到解决,反而变得更加严重。在设置<DataGrid.CellStyle>之后, <Setter Property="BorderThickness" Value="1,1,1,1"/> <Setter Property="BorderBrush" Value="Black"/> 边框变得更加厚,现在等于2像素 - kostey2204
只需将1,1,1,1更改为1,0,0,1,看看是否有所帮助。 - Mohit S
设置BorderThickness值为"1,0,0,1"并没有起到作用,网格线仍然保持原来的状态(如我问题中所示的图片)。 - kostey2204

0

试试这个:

    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect BlurRadius="1" Opacity="0" ShadowDepth="0"/>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.CellStyle>

如果这不起作用,请尝试为DataGrid.RowStyle设置相同的属性。

不幸的是没有帮助。 - kostey2204

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接