我创建了一个网格,在这个网格中它包含一个 TextBlock 。当我最大化或调整窗口大小时, TextBlock 的内容不会保留在 Grid 的中心。
尽量保持尽可能短。产品:>
<Grid>
<Grid HorizontalAlignment="Left" Height="46" VerticalAlignment="Top" Width="515">
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">Welcome! Use the functionalities below.</TextBlock>
</Grid>
</Grid>
答案 0 :(得分:0)
默认情况下,网格会自动展开。也就是说,它们占用的面积与允许的面积相同。您的外部网格将占据窗口的整个客户区域,而内部网格将粘贴到外部网格的左上方,就像您编写它一样。如果您希望内部网格居中,那么请执行类似
的操作<Grid>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Height="46" Width="515">
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">Welcome! Use the functionalities below.</TextBlock>
</Grid>
</Grid>
您甚至不必包含水平和垂直对齐,因为网格将自动居中。
如果您只想将TextBlock居中......
<Grid>
<Grid>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Height="46" Width="515">Welcome! Use the functionalities below.</TextBlock>
</Grid>
</Grid>
可以使用TextAlignment属性对文本进行居中。
我希望这会有所帮助