如何缩小Textblock中的字体大小以适应内容的宽度并保持字体宽高比

时间:2013-09-05 14:25:13

标签: c# wpf xaml wpf-controls textblock

这是交易:我有这行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>

我希望缩小字体以适应内容的高度和宽度。 现在发生的是字体缩小,但Viewbox也会水平缩放内容,使文本框的宽度变小。 这是一个例子

Example image

如果我设置Stretch =“Fill”,文本将填充widht,但只缩小字体大小,使字体看起来非常难看Like this

是否可以缩小文本以适应框架内部,以便它使用容器的整个宽度和高度?

3 个答案:

答案 0 :(得分:1)

您不能将视窗框的StretchDirection属性设置为“DownOnly”。此设置会导致内容仅垂直拉伸。

答案 1 :(得分:0)

这是你想要的吗?

<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>

答案 2 :(得分:0)

这里有一个使用第二个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>
相关问题