Mastering WPF DataGrid in a Day: Hour 3 DataGrid Header

In this third hour of the Mastering WPF in 24 Hours series, I will talk about the DataGrid Header visibility and the appearance of the columns header.

Before continuing this article, I highly recommend reading these previous parts:

HeaderVisibility

You can show or hide a DataGrid header using the HeaderVisibility property. The HeaderVisibility has the four values All, Column, None and Row. If you want to show both row and column headers, set the HeaderVisibility to All. None of the values hide both row and column headers. And the Column and Row values display only row and header columns, respectively. See Figure 6.

header visibility
Figure 6

The code snipet in Listing 10 hides both row and column headers of a DataGrid.

 

  1. <DataGrid HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"   
  2.  Height="219" Width="509" Name="McDataGrid" AutoGenerateColumns="True"  
  3.  RowHeaderWidth="10" AlternatingRowBackground="LightGreen"  
  4.  HeadersVisibility="None"/>  
Listing 12

The output looks like Figure 7.

main window
Figure 7

ColumnHeaderStyle

We often must change the appearance of a DataGrid header. That really means the appearance of the DataGrid column headers. The ColumnHeaderStyle property is used to apply a style to all of the DataGrid column headers.

A DataGrid also provides an option to change the appearance of individual column headers using the property DataGridColumn.HeaderStyle that I will explain in later topics.

A Style can be applied to all column headers, or to an individual column header. To apply a Style to an individual header, set the DataGridColumn.HeaderStyle property, that takes precedence over the DataGrid.ColumnHeaderStyle property.

A Style is usually created as a resource of the application or a Page or a Window. The Style is used to define and set a property using the property name and values.

The code listing in Listing 14 creates a Style as a Windows resource. The first Style is targettting the DataGrid and the second Style is targeting the DataGridColumnHeader. The DataGridColumnHeader style sets the Height, Background, Foreground, FontSize and FontFamily properties.

            data grid vs column headers
  1. <Window.Resources>  
  2.     <Style x:Key="DGHeaderStyle" TargetType="{x:Type DataGrid}">  
  3.         <Setter Property="ColumnHeaderStyle" Value="{DynamicResource DGCHeaderStyle}"/>  
  4.     </Style>  
  5.         <Style x:Key="DGCHeaderStyle" TargetType="DataGridColumnHeader">  
  6.         <Setter Property="Height" Value="30"/>  
  7.         <Setter Property="Background" Value="LightBlue" />  
  8.         <Setter Property="Foreground" Value="Black"/>  
  9.         <Setter Property="FontSize" Value="14" />  
  10.         <Setter Property="FontFamily" Value="Calibri" />  
  11.     </Style>  
  12. </Window.Resources>
Listing 13

The code snipet in Listing 15 sets the Style of the DataGrid using the Style property.
  1. <DataGrid x:Name="McDataGrid" Margin="0" AutoGenerateColumns="False" SelectionUnit="Cell"  
  2. Style="{DynamicResource DGHeaderStyle}"
Listing 14

The output looks as in Figure 8.

output look
Figure 8

Summary

In this first article of the Mastering WPF DataGrid series, we saw how to show and hide a DataGrid column header and manage its appearance.

In the next article of this series, I will cover the basics of the DataGrid rows and their formatting.

<<Previous Article                                       Next Article>>


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.