首页 > 解决方案 > WPF/MVVM DataGrid - 即使 GridLinesVisibility 为 False,也显示一条水平网格线

问题描述

我有一个 DataGrid,它在列表的第 9 条记录之后显示一条固定的水平线(网格线?)。滚动列表时,该行保持固定并且不随列表滚动。即使我设置GridLinesVisibility="None"了,我仍然可以看到坐在那里的线路。如果我向 中添加边框DataGridRow,则相应的线似乎停留在行之间的中间,就好像它是网格线或某种分隔线一样。

这是某种已知的人工制品还是我要疯了?

这是我的数据网格:

       <DataGrid Grid.Row="1" 
                  GridLinesVisibility="None"
                  AutoGenerateColumns="False"
                  CanUserAddRows="False" 
                  CanUserDeleteRows="False"
                  IsReadOnly="True"
                  ItemsSource="{ Binding Path=Users }"
                  SelectedItem="{ Binding SelectedUser, Mode=TwoWay }" >   <!-- SelectedIndex="{ Binding SelectedRowIndex }" -->
            <DataGrid.Columns>
                <DataGridTextColumn Header="User ID" Binding="{Binding UserID}" Width="Auto"/>
                <DataGridTextColumn Header="Initials" Binding="{Binding Initials}" Width="Auto"/>
                <DataGridTextColumn Header="Surname" Binding="{Binding Surname}" Width="Auto"/>
                <DataGridTextColumn Header="Given Names" Binding="{Binding GivenNames}" Width="Auto"/>
                <DataGridTextColumn Header="Active" Binding="{Binding Active, Converter={ StaticResource booleanToYesNoConverter} }" Width="Auto">
                    <DataGridTextColumn.CellStyle>
                        <Style TargetType="{x:Type DataGridCell}">
                            <Setter Property="Background" Value="{ Binding Active, Converter={ StaticResource activeInactiveBackgroundColorConverter }}"/>
                        </Style>
                    </DataGridTextColumn.CellStyle>
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>

非常感谢您的帮助。


编辑: 在稍微摆弄这个问题之后,我发现如果我从一个空的数据网格开始,然后一次添加一条记录,那么神秘的线似乎只是显示在记录之间的数据网格的背景颜色列表可见部分的第 9 和第 10 位(视口中的内容)。然后,如果我将数据网格的背景颜色设置为透明,则该行不再显示:

    <Style TargetType="DataGrid">
        <Setter Property="Background" Value="Transparent"/>
    </Style>

这真的很奇怪。有人对此有任何解释吗?

标签: wpfdatagrid

解决方案


这可能是由于布局数学造成了一个小的差距。尝试设置UseLayoutRounding="True"和/或SnapsToDevicePixels="True"在您的 DataGrid 上查看它是否会使问题消失。

在我共享的样式中,我设置UseLayoutRounding="True"了我Window的 s 以消除模糊的线条和其他布局问题。


推荐阅读