You'll have to fight the DataGrid
a bit to do this but it is possible.
The first thing you want to do is to stop any screen space being reserved for the headers at all. You can do this by setting their Visibility
property to Collapsed
.
<Style TargetType="DataGridColumnHeader">
<Setter Property="Visibility" Value="Collapsed" />
</Style>
If you're declaring columns explicitly you'll need to set their MinWidth
value to 0
in the XAML otherwise, if they're being auto-generated, you'll need to loop them programmatically and set it there - I used the Loaded event. This default was set to 20.
private void StringRepresentationGrid_OnLoadedRepresentationGrid_OnLoaded(object sender, RoutedEventArgs e)
{
foreach (var column in StringRepresentationGrid.Columns)
{
column.MinWidth = 0;
}
}
Then you'll need to override the Template
property of DataGridCell
to something like the below. I've used a monospaced font to keep the layout regular.
<Style x:Key="CustomDataGridCellStyle" TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="TextBlock.TextAlignment" Value="Center"/>
<Setter Property="FontSize" Value="12" />
<Setter Property="Border.BorderThickness" Value="0" />
<Setter Property="TextBlock.FontFamily" Value="Consolas" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBlock
Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This should give you something close to the spacing of a standard TextBlock
in the DataGrid
.