1
Part -1
In your header template, you have a Grid
with two columns: one with width Auto
and the other with *
. The TextBox
placed in the second column is not taking up the remaining space as expected.
By default, the header of a DataGrid column may not be stretched to occupy the full available width. This is because the DataGridColumnHeader
’s HorizontalContentAlignment
property might be set to Center
(or another value) instead of Stretch
.
Override the default header style for your DataGrid so that its column headers have their content stretched. For example, add a style for DataGridColumnHeader
:
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</DataGrid.ColumnHeaderStyle>
DataGrid with the header template might look:
<DataGrid>
<!-- Check the header stretches -->
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FileName}"
HeaderTemplate="{StaticResource FileNameHeaderTemplate}" />
</DataGrid.Columns>
</DataGrid>
And your header template remains:
<DataTemplate x:Key="FileNameHeaderTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Filename" />
<TextBox Grid.Column="1" HorizontalAlignment="Stretch" />
</Grid>
</DataTemplate>
Now, the TextBox
should correctly take up the remaining space in the header.

0
Hi David,
Your issue is likely due to the TextBox
not stretching properly inside the Grid
. Try the following changes:
<DataTemplate x:Key="FileNameHeaderTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Filename" />
<TextBox Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
MinWidth="50" />
</Grid>
</DataTemplate>
If the issue persists, check if DataGrid.CanUserResizeColumns="True"
is causing unexpected behavior.
Thank you!