c# - 如何缩小文本 block 中的字体大小以适应内容的宽度并保持字体纵横比

标签 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",文本将填充宽度,但仅缩小高度中的字体大小,使字体看起来非常难看 Like this

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

最佳答案

继续 Marcus 的工作:

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

我把它改成了

Stretch="Uniform"

StretchDirection="Both"

结果看起来很棒,没有垂直拉伸(stretch)单词。

关于c# - 如何缩小文本 block 中的字体大小以适应内容的宽度并保持字体纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18638968/

相关文章:

c# - 处理看起来像查询字符串的字符串的最简单方法是什么?

c# - 在 Visual Studio 2010 中为 RavenDB 配置/安装 log4net

c# - 如何使用 Visual Studio 2010 将 Windows 控制台应用程序 C# 项目更改为 Windows 服务?

xaml - CalendarDatePicker 在第二次选择相同的日期时返回 null

silverlight - Silverlight 中自定义行为和触发器的设计时问题

c# - Html Agility Pack - 从 html 文档中获取 html 片段

c# - 如何使用单独的 WpfControlLibrary 中的 ResourceDictionary 将样式设置为 Prism 6 模块中的 RadioButton?

C# WPF 自定义控件在设计时不响应 XAML 属性?

WPF:有没有办法在 MultiValueConverter 的 ConvertBack 方法中获取原始值?

c# - 如何创建一个完全不可见的按钮?