WPF TextBlock font resize to fill available space in a Grid WPF TextBlock font resize to fill available space in a Grid wpf wpf

WPF TextBlock font resize to fill available space in a Grid


Wrap the TextBlock inside a ViewBox:

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


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>


I found a great way to do this using ViewBox:

 <Grid>    <Grid.RowDefinitions>        <RowDefinition Height="*" />        <RowDefinition Height="Auto" />        <RowDefinition Height="*" />        <RowDefinition Height="50" />    </Grid.RowDefinitions>    <Grid.ColumnDefinitions>        <ColumnDefinition Width="*" />        <ColumnDefinition Width="100" />    </Grid.ColumnDefinitions>    <Viewbox Grid.Row="0" Grid.Column="0" Stretch="Uniform">        <TextBlock Name="tbTest" Background="Yellow" Text="This is some text" />        </Viewbox>    <ContentControl Grid.Column="0" Grid.Row="2">        <TextBlock>This is some text</TextBlock>    </ContentControl></Grid>