首页 > 解决方案 > DataGrid WPF:中间的灰线

问题描述

正如您在我的屏幕截图中看到的,我的 DataGrid 中间有一条灰线,在网格的第 20 个元素之后。只有当有足够的数据时才会发生这种情况,并且不会随着元素移动:它只是在滚动时停留在那里。

在此处输入图像描述

我一直在搜索可以激活此功能的选项,但找不到类似的东西。可能我的搜索词是错误的,这就是我问的原因。

我已经尽可能地简化了,这是我的 XAML:

<Grid Margin="5,10,5,5" MaxHeight="600" MinWidth="800" MaxWidth="800">
    <DataGrid Name="DataGridTest" GridLinesVisibility="None">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
        </DataGrid.Columns>
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>
</Grid>

这是我的 C#:

public class TestData
{
    public string Name { get; set; }
}

public partial class Hello : Window
{
    public Hello()
    {
        InitializeComponent();

        List<TestData> testData = new List<TestData>();

        for (var i = 0; i < 25; i++)
            testData.Add(new TestData { Name = ("name" + i) });

        DataGridTest.ItemsSource = testData;
    }
}

我在桌面 WPF 应用程序上使用 .NET 5。

感谢您提供任何可能有帮助的见解!

标签: c#wpfdatagridline

解决方案


好的,所以在摆弄了几个小时之后,这肯定是由于尺寸。我不知道怎么做,但我有一些嵌套的 StackPanels 也会影响这种行为。试图用网格替换它们,嵌套它们时也出现了同样的问题。我从头开始重写了所有内容,它开始工作,所以我猜一些属性是错误的,并且 DataGrid 可能对此非常敏感。仍然不知道究竟是什么属性导致了这种情况,但万一有人遇到类似问题:尽量简化到最大值!


推荐阅读