DataGrid.ColumnHeaderStyle Property

Definition

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

public:
 property System::Windows::Style ^ ColumnHeaderStyle { System::Windows::Style ^ get(); void set(System::Windows::Style ^ value); };
public System.Windows.Style ColumnHeaderStyle { get; set; }
member this.ColumnHeaderStyle : System.Windows.Style with get, set
Public Property ColumnHeaderStyle As Style

Property Value

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.

Examples

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

Remarks

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.

Applies to

See also