Search code examples
wpfdatagrid

WPF cannot remove right border for DataGrid cell


I'm using DataGrid to show some information. However every row's right column displays an extra border line which does not align with the DataGrid border.

enter image description here

I've tried to set the cell BorderThickness to 0, but the right border line still exists. The style xaml code is as below.

<!-- DataGrid Cell -->
<Style TargetType="{x:Type DataGridCell}" x:Key="RightCell">
<Setter Property="BorderThickness" Value="0 0 0 0"/>
</Style>
<!--Text Block-->
<Style TargetType="{x:Type TextBlock}" x:Key="RegularTextBlock">
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="FontSize" Value="{StaticResource FontSizeSuperSmall}" />
</Style>

The UI xaml code is as below.

<!-- DataGrid -->
<DataGrid Grid.Column="0" 
Margin="0,0,10,0" 
x:Name="ResultDataGrid"
ItemsSource="{Binding ResultList, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserSortColumns="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
GridLinesVisibility="All"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
HorizontalAlignment="Center"
RowHeaderWidth="0"
BorderBrush="{StaticResource CustomPureBlackBrush}"
BorderThickness="1"
Background="{StaticResource CustomPureWhiteBrush}">
<!-- Set Columns -->
<DataGrid.Columns>
<!-- First two columns have been deleted for simplicity.-->
<!-- Type Column-->
<DataGridTemplateColumn Header="Type" Width="*" IsReadOnly="True" HeaderStyle=" 
   {StaticResource FlattenRightHeader}" CellStyle="{StaticResource RightCell}" >
  <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
          <Grid>
              <TextBlock Name="BuyTYpe" Text="test" Style="{StaticResource RegularTextBlock}" />
          </Grid>
      </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

Could someone give me some suggestions? Thanks.


Solution

  • I believe it is caused by the grid lines. Try turning them off:

    GridLinesVisibility="None"
    

    If you need those lines, you can use styles instead.