0
votes

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?

1
You can't align text to verticle center stackoverflow.com/questions/17828417/…MichaelS

1 Answers

0
votes

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