WPF DataGrid标头列具有额外的边框

时间:2018-08-27 15:12:32

标签: wpf datagrid wpfdatagrid

我是WPF的新手,我想在WPF DataGrid中显示数据库中的数据。一切顺利,除了行的标题和右上角有一些额外的边框(我不知道名字)。我不知道如何隐藏它。 Xaml文件中的代码如下

<Grid Grid.Row="0"  VerticalAlignment="Bottom" FlowDirection="RightToLeft" Background="{DynamicResource brushWatermarkBackground}">
                            <DataGrid Name="DgDeviceList" ItemsSource="{Binding}" AutoGenerateColumns="False" GridLinesVisibility="None" HorizontalAlignment="Stretch" ></DataGrid></Grid>

然后我在App.xaml中有这样的样式:

<Style TargetType="{x:Type DataGrid}">
        <Setter Property="Margin" Value="0,0,0,10" />
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="FontSize" Value="15"/>
        <Setter Property="FontWeight" Value="Normal"/>
    </Style>
    <Style TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="Margin" Value="0"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="FontSize" Value="15"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="SeparatorBrush" Value="Transparent"></Setter>
    </Style>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Margin" Value="0"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="BorderThickness" Value="0,0,1,0"/>
        <Setter Property="FontSize" Value="15"/>
    </Style>

我在CS文件中填充Datagrid的方式如下:

 DgDeviceList.Columns.Clear();
            DgDeviceList.Items.Refresh();
            DataTable dt = AccessDb.GetDeviceList();
            if (dt != null)
            {
                DgDeviceList.DataContext = dt;
                DataGridTextColumn c1 = new DataGridTextColumn();
                Binding b = new Binding("DeviceName");
                c1.Binding = b;

                c1.Header = "Device Name";
                DgDeviceList.Columns.Add(c1);
                DataGridTextColumn c2 = new DataGridTextColumn();
                Binding b2 = new Binding("DeviceSIM1");
                c2.Binding = b2;

                c2.Header = "SIM Card1";
                DgDeviceList.Columns.Add(c2);
                DataGridTextColumn c3 = new DataGridTextColumn();
                Binding b3 = new Binding("DeviceSIM2");
                c3.Binding = b3;

                c3.Header = "SIM Card 2";
                DgDeviceList.Columns.Add(c3);
                DataGridTextColumn c0 = new DataGridTextColumn();
                Binding b0 = new Binding("PIN");
                c0.Binding = b0;

                c0.Header = "Pin Code";
                DgDeviceList.Columns.Add(c0);
                DataGridTextColumn c4 = new DataGridTextColumn();
                Binding b4 = new Binding("MoreInfo");
                c4.Binding = b4;

                c4.Header = "More Info";
                DgDeviceList.Columns.Add(c4);
            }
        }
        catch (Exception ex)
        {
            LogEvents.InLogFile("FillDataGrid:" + ex.Message);
            txtLog.Text = ex.Message + "\n" + txtLog.Text;
        }

输出数据网格如下图所示。 enter image description here

它在行和列分隔符中有一个正方形,我认为这是为了向我们展示行和列可调整大小。

编辑 添加HeadersVisibility =“ Column”后,右下角与下图相同。 enter image description here

1 个答案:

答案 0 :(得分:0)

只需将标题可见性添加到数据网格

<DataGrid HeadersVisibility="Column" Name="DgDeviceList" ItemsSource="{Binding}" AutoGenerateColumns="False" GridLinesVisibility="None" HorizontalAlignment="Stretch" />