Adding padding arount Items container in DataGrid wpf

45 views Asked by At

I want to add left and right padding/margin for items in DataGrid. I've tried various of methods, searched a lot, asked AI and nothing have worked out. More specifically:

  • I want only 'container' containing items inside DataGrid to have padding (not all individual cells, because I'd have to somehow distinguish where to which side of them add that spacing)
  • I don't want column headers to have that padding/margin
  • I don't want scrollbar to appear (which always happend when I tried to modify default ControlTemplate in many different ways)
  • I want items inside to shrink depending on how much padding you give to the containter
  • Preferably, I want to do this only in xaml

Picture explaining what I want exactly

<DataGrid
    ItemsSource="{Binding Leaderboard.PlayersScores}" <!--list of objects with Name and BestScore-->
    AutoGenerateColumns="False" 
    RowHeaderWidth="0" 
    CanUserResizeColumns="False"
    CanUserResizeRows="False"
    BorderThickness="0"
    
    <!--[...]-->

    RowStyle="{StaticResource DataGridRowStyle}"  <!-- deleting margin,padding,border -->
    CellStyle="{StaticResource DataGridCellStyle}" <!-- controlTemplate for items,background -->
    ColumnHeaderStyle="{StaticResource DataGridColumnHeaderStyle}" <!-- some font,margin,padding-->
    Style="{StaticResource DataGridStyle}"   <!-- deleting margin -->
    Padding="1"
    >
    
    <!-- I wish I could just add some border with padding around that -->
    <DataGrid.Columns >
        <DataGridTextColumn Width="*" Header="Nickname" Binding="{Binding Name}"/>
        <DataGridTextColumn Width="*" Header="BestScore" Binding="{Binding BestScore}"/>
    </DataGrid.Columns>
    

</DataGrid>

If you want full code from that file you can check out my repo (I am learning basics of wpf now so I do realise that it looks terrible)

I've tried:

  • Adding padding/margin to every possible element of it
  • ControlTemplate with border /grid&border/[many other combinations] around ItemsPresenter ScrollViewer
  • Asking chatgpt to do that, didn't work
  • many other
0

There are 0 answers