0

I am building this table on WPF .NET 8

<DataGrid x:Name="dataGrid"
      Width="640"
      ClipToBounds="True"
      AutoGenerateColumns="False"
      GridLinesVisibility="All"
      HeadersVisibility="Column"
      RowHeaderWidth="0"
      IsReadOnly="True"
      CanUserAddRows="False"
      CanUserDeleteRows="False"
      CanUserReorderColumns="False"
      CanUserResizeColumns="False"
      CanUserResizeRows="False"
      HorizontalScrollBarVisibility="Disabled"
      VerticalScrollBarVisibility="Auto"
      ScrollViewer.CanContentScroll="False"
      Background="Transparent"
      BorderThickness="1"
      EnableRowVirtualization="True"
      EnableColumnVirtualization="True"
      VirtualizingPanel.VirtualizationMode="Recycling">

<DataGrid.Resources>
    <!-- Light/dark theme-aware header style -->
    <Style x:Key="BaseHeaderStyle" TargetType="DataGridColumnHeader">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
        <Setter Property="FontWeight" Value="SemiBold" />
        <Setter Property="Padding" Value="5" />
    </Style>
</DataGrid.Resources>


<DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding SourceType}" Header="Print Source" Width="200">
        <DataGridTextColumn.ElementStyle>
            <Style TargetType="TextBlock">
                <Setter Property="HorizontalAlignment" Value="Left" />
            </Style>
        </DataGridTextColumn.ElementStyle>
        <DataGridTextColumn.HeaderStyle>
            <Style TargetType="DataGridColumnHeader" BasedOn="{StaticResource BaseHeaderStyle}">
                <Setter Property="HorizontalContentAlignment" Value="Left" />
            </Style>
        </DataGridTextColumn.HeaderStyle>
    </DataGridTextColumn>

    <DataGridTextColumn Binding="{Binding PrintedAt}" Header="Printed At" Width="250" CanUserSort="False">
        <DataGridTextColumn.ElementStyle>
            <Style TargetType="TextBlock">
                <Setter Property="HorizontalAlignment" Value="Center" />
            </Style>
        </DataGridTextColumn.ElementStyle>
        <DataGridTextColumn.HeaderStyle>
            <Style TargetType="DataGridColumnHeader" BasedOn="{StaticResource BaseHeaderStyle}">
                <Setter Property="HorizontalContentAlignment" Value="Center" />
            </Style>
        </DataGridTextColumn.HeaderStyle>
    </DataGridTextColumn>

    <DataGridTextColumn Binding="{Binding PageCount}" Header="Pages Printed" Width="170">
        <DataGridTextColumn.ElementStyle>
            <Style TargetType="TextBlock">
                <Setter Property="HorizontalAlignment" Value="Right" />
            </Style>
        </DataGridTextColumn.ElementStyle>
        <DataGridTextColumn.HeaderStyle>
            <Style TargetType="DataGridColumnHeader" BasedOn="{StaticResource BaseHeaderStyle}">
                <Setter Property="HorizontalContentAlignment" Value="Right" />
            </Style>
        </DataGridTextColumn.HeaderStyle>
    </DataGridTextColumn>
</DataGrid.Columns>

The table is exactly how I want it to be, however there is a black extra header for some reason and I can't find out how to remove this. I don't want to put the DataGrid in a Border as this doesn't work I already tried that. Any ideas?

It currently looks like this:

table with a black thingy

2
  • You specified a DataGrid Width of 640; and column widths that add up to 620. The DataGrid should be left to size "itself" in this case. Commented Apr 5 at 1:00
  • @GerrySchmitz you are absolutely right. I didn't even notice that. Thanks a lot for pointing this out. Commented Apr 7 at 11:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.