Style each datagrid column header

Mark A picture Mark A · May 20, 2011 · Viewed 7.5k times · Source

I want to add another row to the HeaderRow and for each column insert specific control.
This is my code where add this row, but I can only set same control (in this case textbox) for every column, but I want to set specific control for specific column. (like it's done in ASP.NET repeater)

<Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
           <Grid x:Name="grid" VerticalAlignment="Center" HorizontalAlignment="Stretch"  Background="{TemplateBinding Background}">
              <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
              </Grid.RowDefinitions>
              <TextBlock Name="ColumnHeader" Grid.Row="0" Text="{TemplateBinding Content}"  HorizontalAlignment="Center" VerticalAlignment="Center" Height="18" />
              <Grid Grid.Row="1">
                 <TextBox Text="" HorizontalAlignment="Stretch" BorderThickness="0,1,1,1" />
              </Grid>
          </Grid>
</Style>

Answer

biju picture biju · May 20, 2011

You can Make use of the HeaderTemplate to specify different datatemplates for each Column

Sample

<Window.Resources>
    <DataTemplate x:Key="Template1">
        <Grid x:Name="grid" VerticalAlignment="Center" HorizontalAlignment="Stretch">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <TextBlock Name="ColumnHeader" Text="Student Id" Grid.Row="0"  HorizontalAlignment="Center" VerticalAlignment="Center" Height="18" />
            <Grid Grid.Row="1">
                <TextBox Text="" HorizontalAlignment="Stretch" BorderThickness="0,1,1,1" />
            </Grid>
        </Grid>
    </DataTemplate>
    <DataTemplate x:Key="Template2">
        <Grid x:Name="grid" VerticalAlignment="Center" HorizontalAlignment="Stretch">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <TextBlock Name="ColumnHeader" Text="Student Name" Grid.Row="0"  HorizontalAlignment="Center" VerticalAlignment="Center" Height="18" />
            <Grid Grid.Row="1">
                <TextBox Text="" HorizontalAlignment="Stretch" BorderThickness="0,1,1,1" />
            </Grid>
        </Grid>
    </DataTemplate>
</Window.Resources>
<Grid>
    <DataGrid Name="dataGrid" >
        <DataGrid.Columns>
            <DataGridTextColumn HeaderTemplate="{StaticResource Template1}"  Width="90*" MinWidth="120" Binding="{Binding StudentId}"/>
            <DataGridTextColumn HeaderTemplate="{StaticResource Template2}" Width="90*" MinWidth="120" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Score" Width="100*" MinWidth="150" Binding="{Binding Score}"/>
        </DataGrid.Columns>
    </DataGrid>

</Grid>