Search code examples
c#.netwpfdatagridborder

How to remove double line in datagrid in C#


First, I want to draw the border of the entire datagrid.

so I set up datagrid like the code below

<DataGrid ItemsSource="{Binding DirectoryPath, Source={StaticResource vm}}"
          CanUserAddRows="False"
          RowHeaderWidth="0"
          BorderBrush="Black"
          BorderThickness="1"
          AutoGenerateColumns="False">

And, I added DataGridTemplateColum

<DataGridTemplateColumn Header="Pattern List" MinWidth="100" Width="*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=PathDir}"
                       HorizontalAlignment="Left"
                       VerticalAlignment="Center"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

In the Run Screen, I found that a double line occurs.

My question is two,

  1. How to remove the double line in the yellow circle?

  2. Can I change the line color in DataGridTemplateColum's Header in the red circle?

Image


Solution

  • You can change the DataGrid header design as follows. The following is a simple example. For more detailed information, please see.

    <Window.Resources>
    
        <LinearGradientBrush x:Key="DataGridHeaderBrush" StartPoint="0.5,0" EndPoint="0.5,1">
            <GradientStop Color="#2a5298" Offset="0"/>
            <GradientStop Color="#1e3c72" Offset="1"/>
        </LinearGradientBrush>
    
    
        <Style x:Key="DataGridHeaderStyle" TargetType="DataGridColumnHeader">
            <Setter Property="Background" Value="{StaticResource DataGridHeaderBrush}" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="BorderBrush" Value="Black" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="HorizontalContentAlignment" Value="Left" />
            <Setter Property="MinWidth" Value="0" />
            <Setter Property="MinHeight" Value="30" />
            <Setter Property="Cursor" Value="Hand" />
        </Style>
        </Window.Resources>
    
    <Grid >
        <DataGrid Name="dataGrid1" ColumnHeaderStyle="{StaticResource DataGridHeaderStyle}"/>
    </Grid>
    

    I don't understand why the thickness in the yellow area is visible. Everything seems normal to me.