Search code examples
wpffontsresizetextblock

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?


Solution

  • Wrap the TextBlock inside a ViewBox:

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