2
votes

Here's the deal: I have this line of XAML

<Viewbox Canvas.Left="27" Canvas.Top="479" Width="377" Height="21" Stretch="Fill" 
       StretchDirection="DownOnly" VerticalAlignment="Top" HorizontalAlignment="Left">
  <TextBlock Name="TitleText" TextWrapping="Wrap" TextAlignment="Left" FontSize="14" 
           FontStretch="Normal"  FontStyle="Normal" Foreground="White" Width="377" >some long text here
  </TextBlock>
</Viewbox>

I want the font to scale down to fit the contents height and width. What happens now is that the font scales down, but the Viewbox also scales the content horizontally, making the width of the textbox smaller. Here's an example

Example image

If I set Stretch="Fill" the text will fill the widht, but shrinks only font-size in heigth, making the font look horribly ugly Like this.

Is it possible to shrink the text to fit inside a frame so that it uses the whole width and height of the container?

3
It sounds like maybe you just want the text to wrap to the full width of the available space, without changing the font size? Or alternatively, to drop the font size to allow the text to fit without scaling the entire control. I'm not sure that a Viewbox is the best solution for this.Dan Puzey
It sounds like you just need a multiline TextBox with a ScrollBar to me... your end users won't thank you for messing with the size of the text... it will make it inconsistent with the rest of the text in the application and for that matter, each text object (I imagine) would be inconsistently sized compared to each other.Sheridan

3 Answers

1
votes

You may not set the StretchDirection property of your viewbox to "DownOnly". This setting leads to the effect that the content only gets stretched vertical.

0
votes

Is this what you want?

<Viewbox Stretch="Uniform">
    <TextBlock Name="TitleText" TextWrapping="Wrap" TextAlignment="Left" FontSize="14" 
       FontStretch="Normal"  FontStyle="Normal" Foreground="White" Width="377">
       some long text here
    </TextBlock>
</Viewbox>
0
votes

Here there is a possible solution using a second ViewBox:

<Viewbox Canvas.Left="27" Canvas.Top="479" Width="377" Height="21" Stretch="Fill" 
        StretchDirection="DownOnly" VerticalAlignment="Top" HorizontalAlignment="Left">
   <Viewbox Stretch="Fill" Width="Auto" Height="Auto">
       <TextBlock Name="TitleText" TextWrapping="Wrap" TextAlignment="Left" FontSize="14" 
            FontStretch="Normal"  FontStyle="Normal" Foreground="White" Width="377" >some long  text here
       </TextBlock>
   </Viewbox>
 </Viewbox>