user7157732
user7157732

Reputation: 347

DataGrid Columnheader Style - WPF C#

Created two separate Datagrids; one without columnheader style and another one with columnheader style

DataGrid 1:

<DataGrid x:Name="PrintReport" ItemsSource="{Binding MonthlyResults}" AutoGenerateColumns="False" FontFamily="Tahoma" FontSize="12" IsReadOnly="True" CanUserSortColumns="False" HorizontalContentAlignment="Stretch"  VerticalContentAlignment="Stretch"  AlternatingRowBackground="Gainsboro"  AlternationCount="1" SelectionMode="Extended" SelectionUnit="Cell" >
  <DataGrid.Columns>
      <DataGridTextColumn Header="Pattern" Binding="{Binding }"   >
        <DataGridTextColumn.ElementStyle>
          <Style>
             <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
             <Setter Property="TextBlock.TextAlignment" Value="Center"/>
          </Style>
        </DataGridTextColumn.ElementStyle>
      </DataGridTextColumn>
      <DataGridTextColumn Header="Lease" Binding="{Binding VOD,StringFormat={}{0:0.00}}"  >
        <DataGridTextColumn.ElementStyle>
           <Style>
            <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
            <Setter Property="TextBlock.TextAlignment" Value="Center"/>
          </Style>
       </DataGridTextColumn.ElementStyle>
     </DataGridTextColumn>
  </DataGrid.Columns> 
</DataGrid>

DataGrid 2:

 <DataGrid x:Name="PrintReport2" ItemsSource="{Binding MonthlyResults}" AutoGenerateColumns="False" FontFamily="Tahoma" FontSize="13" IsReadOnly="True" CanUserSortColumns="False" HorizontalContentAlignment="Stretch"  VerticalContentAlignment="Stretch"  AlternatingRowBackground="Gainsboro"  AlternationCount="1" SelectionMode="Extended" SelectionUnit="Cell">  

<DataGrid.Columns>
<DataGridTextColumn Header="Req - OAR" Binding="{Binding }"   >
<DataGridTextColumn.ElementStyle>


     <Style>
     <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
    <Setter Property="TextBlock.TextAlignment" Value="Center"/>
       </Style>
</DataGridTextColumn.ElementStyle>

    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="Background" Value="LightCyan"/>
        </Style>
                                                            </DataGridTextColumn.HeaderStyle>
    </DataGridTextColumn>  

Basic difference being the DATAGRID 2 column headers have "background color" applied to them. The display that I get looks something like this:

enter image description here

Datagrid2 clumn header shows "extra blank space" below the column header?? How do I remove it?

Upvotes: 1

Views: 4453

Answers (1)

rmojab63
rmojab63

Reputation: 3631

You are overriding the HeaderStyle. Just use the default style and adjust other parts too:

 <DataGridTextColumn.HeaderStyle>
      <Style TargetType="{x:Type DataGridColumnHeader}">
      <Setter Property="Background" Value="LightCyan"/>
      <Setter Property="Foreground" Value="#FF000000" />
      <Setter Property="HorizontalContentAlignment" Value="Left" />
      <Setter Property="VerticalContentAlignment" Value="Center" />
      <Setter Property="IsTabStop" Value="False" />
      <Setter Property="SeparatorBrush" Value="#FFC9CACA" />
      <Setter Property="Padding" Value="4" />
  </Style>
</DataGridTextColumn.HeaderStyle>

Note that regarding the extra space, the effective part is the Padding.

Edit note that You can used BasedOn to inherit default style. However, for that to work, you should declare it under DataGrid resources section. see this answer.

Upvotes: 2

Related Questions