WPF/Silverlight XAML Stretch Text Size to Fit container?

烂漫一生 提交于 2019-11-30 13:39:38

问题


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


回答1:


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>



回答2:


Depends on the parent container

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




回答3:


Set HorizonalAlignment/VerticalAlignment to "stretch".




回答4:


Use DockPanel as parent container

<DockPanel>
  <TextBlock />
</DockPanel>


来源:https://stackoverflow.com/questions/981067/wpf-silverlight-xaml-stretch-text-size-to-fit-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!