I would like to set DataGrid
cell's padding, I have tried both major solutions from the widely accepted question Set a padding on dataGridCells in WPF.
However none of the solutions work properly, they cause auto-sized columns to collapse to header's width:
How can I set cell padding so that it does not break the DataGrid
column's auto sizing?
Code sample:
<DataGrid IsReadOnly="True" CanUserResizeColumns="False" AutoGenerateColumns="False"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<DataGrid.ItemsSource>
<x:Array Type="{x:Type sys:DateTime}">
<sys:DateTime/>
</x:Array>
</DataGrid.ItemsSource>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Padding" Value="10,2"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Some short header" Binding="{Binding Path=., StringFormat=dddd MMMM yyyy HH:mm:ss}"/>
<DataGridTextColumn Header="Stretching" Binding="{Binding}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
Note: the issue appears consistently only during runtime, in editor window it looks OK after making any changes, until building the solution and/or running the app that is.