WPF TextBlock font resize to fill available space in a Grid

Nate Zaugg picture Nate Zaugg · Feb 17, 2010 · Viewed 52.4k times · Source

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?

Answer

Jobi Joy picture Jobi Joy · Feb 17, 2010

Wrap the TextBlock inside a ViewBox:

   <Grid>
    <Viewbox>
        <TextBlock TextWrapping="Wrap" Text="Some Text" />
    </Viewbox>
   </Grid>