调整标签fontsize

时间:2014-05-18 17:11:30

标签: c# wpf label

我想动态调整标签内容;当文本较大时,调整标签fontsize以适合标签空间。

<Label Name="lblResult" Content="0" Margin="0,-1,-222,0" VerticalAlignment="Top"
       HorizontalContentAlignment="Right" VerticalContentAlignment="Bottom"
       FontFamily="Helvetica Neue" Foreground="White" Background="Black"
       Height="87" FontSize="63" RenderTransformOrigin="0.5,0.5" Padding="9"
       ContentStringFormat="{}{0:#,0}" FontWeight="Light" ClipToBounds="True" />

谢谢

1 个答案:

答案 0 :(得分:0)

WPF Viewbox控件会将其内容增大/缩小到可用空间:

http://www.wpftutorial.net/ViewBox.html

只需将您的标签放在ViewBox中:

<Viewbox Stretch="Uniform">
    <Label Name="lblResult" Content="Moez Rebai" VerticalAlignment="Top"
   HorizontalContentAlignment="Right" VerticalContentAlignment="Bottom"
   FontFamily="Helvetica Neue" Foreground="White" Background="Black"
   Height="87" FontSize="63" RenderTransformOrigin="0.5,0.5" Padding="9"
   ContentStringFormat="{}{0:#,0}" FontWeight="Light" ClipToBounds="True" />
</Viewbox>

当然,您的Viewbox通常按容器缩放,但希望您明白这一点!