Get a textbox to fill a grid cell in WPF

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?


如果您使用标签而不是TextBlock,则可以将文本排列为中心。

链接地址: http://www.djcxy.com/p/50074.html

上一篇: 坚持StackPanel的底部

下一篇: 获取一个文本框来填充WPF中的网格单元格