I'm trying to display a textbox in WPF that occupies the entire space of its containing grid cell.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Content="1" HorizontalAlignment="Center"/>
<DockPanel Grid.Column="1" VerticalAlignment="Stretch" >
<TextBlock
Text="2"
Background="Black" Foreground="White"
TextAlignment="Center" VerticalAlignment="Center"
/>
</DockPanel>
<Button Grid.Column="2" Content="3" HorizontalAlignment="Center"/>
</Grid>
I've tried having the textblock in the grid directly, and various other containers: DockPanel, UniformGrid, StackPanel. The closest I've got is when I have the textblock in the grid directly and set the VerticalAlignment
to Stretch
, but that leaves the text aligned to the top of the textblock.
So, my question is: how can I (or is it possible to) force the middle textblock to fill the available grid space, and centralise the text in the textblock?