0

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?

Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
  • You can't align text to verticle center http://stackoverflow.com/questions/17828417/centering-text-vertically-and-horizontally-in-textblock-and-passwordbox-in-windo – MichaelS Dec 07 '14 at 13:24

1 Answers1

0

If you use a label instead of a TextBlock you can arrange the text to be central.

kidshaw
  • 3,423
  • 2
  • 16
  • 28