Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF TextBlock font resize to fill available space in a Grid

I have some text that is displayed at run time in a textblock. I want the font size to be the biggest it can be to fill the area that is given. I think I have the textblock setup correctly to "autosize" and I try to increase the font size till the textblock is bigger than than its parent then decrease the font size by 1. The problem is I can't get the control to redraw/recompute its size.

Is the a better way to do that? Or is there a way I can make my method work?

like image 784
Nate Zaugg Avatar asked Feb 17 '10 16:02

Nate Zaugg


2 Answers

Wrap the TextBlock inside a ViewBox:

   <Grid>     <Viewbox>         <TextBlock TextWrapping="Wrap" Text="Some Text" />     </Viewbox>    </Grid> 
like image 57
Jobi Joy Avatar answered Sep 23 '22 01:09

Jobi Joy


I had the same problem. You can use this to resize the fontsize of the textblock to fill the area when it has overflow.


<Viewbox StretchDirection="DownOnly" Stretch="Uniform">     <TextBlock Text="{Binding Path=Title}" HorizontalAlignment="Center"/> </Viewbox> 

like image 21
alireza alimardan Avatar answered Sep 24 '22 01:09

alireza alimardan