我刚刚开始使用WPF。

是否可以具有Label或TextBlock大小的文本大小本身来填充其父容器?

谢谢,
标记

最佳答案

您可以使用ViewBox可视地缩放某些内容以适合其容器。这里的其他解决方案都可以,但是它们只能扩展控件,而不能扩展其内容。 ViewBox将同时拉伸(stretch)两者。

<!-- Big grid, will stretch its children to fill itself -->
<Grid Width="1000" Height="1000">
 <!-- The button is stretched, but its text remains teeny tiny -->
 <Button>
  <!-- The viewbox will stretch its content
  to fit the final size of the button -->
  <Viewbox
      Margin="4"
      VerticalAlignment="Stretch"
      Height="Auto">
      <!-- The textblock and its contents are
      stretched to fill its parent -->
      <TextBlock
          Text="Bartenders" />
  </Viewbox>
 </Button>
</Grid>

10-05 20:21