WPF/Silverlight XAML Stretch Text Size to Fit cont

2019-02-05 14:00发布

I've just started to play with WPF.

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

Thanks, Mark

4条回答
混吃等死
2楼-- · 2019-02-05 14:26

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.

<!-- 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>
查看更多
做个烂人
3楼-- · 2019-02-05 14:27

Depends on the parent container

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

查看更多
SAY GOODBYE
4楼-- · 2019-02-05 14:27

Set HorizonalAlignment/VerticalAlignment to "stretch".

查看更多
相关推荐>>
5楼-- · 2019-02-05 14:32

Use DockPanel as parent container

<DockPanel>
  <TextBlock />
</DockPanel>
查看更多
登录 后发表回答