I found the answer. I should use <DataGridColumnHeadersPresenter />
instead of <ContentPresenter />
. So my code that works looks like:
<DataGrid ItemsSource="{Binding List}" AutoGenerateColumns="True">
<DataGrid.Template>
<ControlTemplate>
<StackPanel Orientation="Vertical">
<TextBlock>before</TextBlock>
<DataGridColumnHeadersPresenter />
<TextBlock>after</TextBlock>
<ItemsPresenter />
</StackPanel>
</ControlTemplate>
</DataGrid.Template>
</DataGrid>