How to force zero pixel gap between consecutive cells in WPF DataGrid

I am building an application in WPF for special needs. At first I selected user controls, but later found out that a lot of what I needed was already implemented in Datagrid Control. However, there is one small glitch:

The problem with Datagrid is that it provides a minimum of 2 pixel gaps between two consecutive cells (1 on each side of the grid). Please look at the following diagram for clarity:

. Note the 2-pixel difference set by the Datagrid between two consecutive cells: http://img265.imageshack.us/img265/3545/figurem.png

(Stack overflow did not allow me to add an image to my question with a link to the anti-spam policy for new users)

.

This does not meet my requirement, since I want the content to be displayed “continuously” (there shouldn't be this 2 pixel gap, I want the connecting lines to look “connected”). I tried fidgeting with GridLinesVisibility, but that didn't help. The DataGrid has a custom control, for example:

<DataGrid.Columns> <DataGridTemplateColumn Width="25" Header=""> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ContentControl Content="{Binding Path=MyCustomControl}" Margin="0"></ContentControl> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> .... </DataGrid.Columns> 

I have so far tried:

but nothing appears.

Is there a solution for this / some hack or workaround or will I need to create everything from scratch? I have decent experience with C #, but I'm new to WPF.

Please, help.

+6
source share
1 answer

What you need to do is get the DataGridCell style for your DataGrid and set its BorderThickness value to 0. It is hardcoded as 1 in the default style, but, fortunately, it's easy to override this:

 <Style TargetType="DataGridCell"> <Setter Property="BorderThickness" Value="0" /> </Style> 

I would suggest putting this in the resources of your DataGrid, so it only affects one grid if you don't want it to have a wider scope.

 <DataGrid> <DataGrid.Resources> <Style TargetType="DataGridCell">...</Style> </DataGrid.Resources> ... </DataGrid> 

There are other places that you could place, depending on your specific needs.

+4
source

Source: https://habr.com/ru/post/900421/


All Articles