WPF/Silverlight XAML 拉伸文本大小以适合容器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/981067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:33:56  来源:igfitidea点击:

WPF/Silverlight XAML Stretch Text Size to Fit container?

wpfsilverlighttextsize

提问by Mark

I've just started to play with WPF.

我刚刚开始玩 WPF。

Is it possible to have the size of the text of a Label or TextBlock size itself to fill it's parent container?

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

Thanks, Mark

谢谢,马克

回答by

You can use a ViewBox to visually zoom something to fit within its container. The other solutions here work, but they only stretch the control, not its content. The ViewBox will stretch both.

您可以使用 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>

回答by Arcturus

Depends on the parent container

依赖于父容器

Grid, DockPanel will stretch your control StackPanel, WrapPanel will leave it to the control to size itself..

网格,DockPanel 将拉伸您的控件 StackPanel,WrapPanel 将它留给控件来调整自己的大小..

回答by Simon P Stevens

Set HorizonalAlignment/VerticalAlignment to "stretch".

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

回答by Andrija

Use DockPanel as parent container

使用 DockPanel 作为父容器

<DockPanel>
  <TextBlock />
</DockPanel>