Export (0) Print
Expand All
Expand Minimize

DataGrid.ColumnHeaderStyle Property

Gets or sets the style applied to all column headers in the DataGrid.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

public Style ColumnHeaderStyle { get; set; }
<object>
  <object.ColumnHeaderStyle>
    <Style .../>
  </object.ColumnHeaderStyle>
</object>

Property Value

Type: System.Windows.Style
The style applied to all column headers in the DataGrid. The registered default is null. For more information about what can influence the value, see DependencyProperty.

Apply a Style to update the visual appearance of all the column headers in the DataGrid. To define a Style for a column header, specify a TargetType of DataGridColumnHeader.

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, which takes precedence over the DataGrid.ColumnHeaderStyle property.

The following example uses a style resource to change the style of column headers in the DataGrid. The style of the first column header is then set with an inline style that overrides the DataGrid.ColumnHeaderStyle.

<Window.Resources>
    <!-- DataGrid style -->
    <Style x:Key="DataGridStyle1" TargetType="{x:Type DataGrid}">
        <Setter Property="ColumnHeaderStyle" Value="{DynamicResource ColumnHeaderStyle1}"/>
    </Style>
    <!-- DataGridColumnHeader style -->
    <Style x:Key="ColumnHeaderStyle1" TargetType="DataGridColumnHeader">
        <Setter Property="Height" Value="30"/>
        <Setter Property="Background" Value="LightBlue"/>
        <Setter Property="Foreground" Value="Blue"/>
        <Setter Property="FontSize" Value="18" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="ToolTip" Value="Click to sort."/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <DataGrid Name="dataGrid1" Margin="12,12,0,0" 
              AutoGenerateColumns="False"
              Style="{DynamicResource DataGridStyle1}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" 
                                Binding="{Binding Name}">
                <!-- Local Style for header of first DataGrid column. -->
                <DataGridColumn.HeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="Background" Value="Blue"/>
                        <Setter Property="Foreground" Value="White"/>
                        <Setter Property="FontSize" Value="24"/>
                    </Style>
                </DataGridColumn.HeaderStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Class"
                                Binding="{Binding Classification}" />
            <DataGridCheckBoxColumn Header="Extinct"
                                    Binding="{Binding Extinct}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

The following illustration shows the output.

Styled DataGrid Column Headers

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft