29

我刚开始玩WPF。

是否可以使用 Label 的文本大小或 TextBlock 大小本身来填充它的父容器?

谢谢,马克

4

4 回答 4

48

您可以使用 ViewBox 以可视方式缩放某些内容以适应其容器。此处的其他解决方案有效,但它们仅拉伸控件,而不是其内容。ViewBox 将同时拉伸两者。

<!-- 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>
于 2009-06-11T13:46:59.303 回答
3

取决于父容器

Grid,DockPanel 将拉伸您的控件 StackPanel,WrapPanel 会将其留给控件自行调整大小。

于 2009-06-11T13:34:07.150 回答
1

将 Horizo​​nalAlignment/VerticalAlignment 设置为“拉伸”。

于 2009-06-11T13:30:57.250 回答
0

使用 DockPanel 作为父容器

<DockPanel>
  <TextBlock />
</DockPanel>
于 2009-06-11T13:28:53.933 回答