0

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:

datagrid

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.

wondra
  • 3,271
  • 3
  • 29
  • 48

1 Answers1

0

I've tried to simply add this Width="Auto" into the first DataGridTextColumn and it worked for me.

Code:

<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" Width="Auto" Binding="{Binding Path=., StringFormat=dddd MMMM yyyy HH:mm:ss}"/>
            <DataGridTextColumn Header="Stretching" Binding="{Binding}" Width="*"/>
        </DataGrid.Columns>
    </DataGrid>

Result:

enter image description here

Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26
  • Hmmm ...either adding or removing `Width="Auto"` somewhat work - if you run the application and change the source during runtime it re-renders correctly, however on next run its the very same problem. Have you tried building it rather than runtime edit? I also noticed it is likely resolution-dependent, if you try lower value of padding, it works. – wondra Aug 01 '19 at 06:16
  • No, but after giving this answers, I've noticed that if I closed the design indow in the Visual Studio, and then reopen, it becomes with the exact same error – Catarina Ferreira Aug 01 '19 at 08:04