Search code examples
wpfstyleswpfdatagrid

How to derive my own DataGridColumnHeaderStyle from the default DataGridColumnHeader?


I've made a stlye for DataGridColumnHeader, and I want to derive that from the default DataGridColumnHeader, so I do this:

  <Style TargetType="{x:Type DataGridColumnHeader}" x:Key="ColumnHeaderStyle" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
                    <TextBlock Grid.Column="1" Grid.Row="0"  Text="{TemplateBinding Content}" HorizontalAlignment="Center">
                    </TextBlock>
                  ......
               </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

but it's not work, it seems the DataGrid doesn't have the defaultStyle anymore, I think the problem is setting template for DataGridColumnHeader in the code above, but what should I do?


Solution

  • Well,I just needed to set ContentTemplate insted of Template!!

    <Style TargetType="{x:Type DataGridColumnHeader}" x:Key="ColumnHeaderStyle" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
                    <TextBlock Grid.Column="1" Grid.Row="0"  Text="{TemplateBinding Content}" HorizontalAlignment="Center">
                    </TextBlock>
                  ......
               </Grid>
            </DataTemplate>
        </Setter.Value>
    </Setter>