c# - Changing the font size of an element programmatically inside a Viewbox works weird
问题描述
I need to change the font size of a label programmatically. The label is located inside a Viewbox:
<Window ...
FontSize="24"
HorizontalAlignment="Center" VerticalAlignment="Center"
WindowStyle="ThreeDBorderWindow"
ResizeMode="NoResize">
<Grid>
<Viewbox x:Name="vb" Stretch="Uniform">
<Label x:Name="label1" HorizontalContentAlignment="Center" Content="Text"/>
</Viewbox>
</Grid>
</Window>
The font size of text inside the label is assumed to be 24. However, even if I set it in my code like this to its 'original' value in xaml:
label1.FontSize = 24;
it becomes smaller.
Is there a simple way to keep the ratio between the new font size and the scale factor of the Viewbox?
解决方案
You can try to set ViewBox.StretchDirection
property to DownOnly
, for the label will be smaller, but not bigger:
<StackPanel Width="200" >
<Label Content="Text" FontSize="24" HorizontalAlignment="Center"/>
<Viewbox StretchDirection="DownOnly" Stretch="Uniform">
<Label Content="Text" FontSize="24"/>
</Viewbox>
<Viewbox StretchDirection="DownOnly" Stretch="Uniform">
<Label Content="ABCDEF EEEEE ABCDEFGJKLMNOP" FontSize="24"/>
</Viewbox>
</StackPanel>
推荐阅读
- perforce - Perforce 错误 - 警告:无法从非流客户端标题提交
- r - 两个不同向量之间成对比较的距离矩阵
- python - 如何将三个模型合并在一起,并使用 .all() 从 django 中的这三个模型中获取所有帖子?
- android - 更改弹出菜单文本颜色
- android - 在 Android Espresso 测试中处理第三方键盘?
- sentry - 什么应该是 Sentry for React 应用程序中的事务
- mule - 如何从 Mule4 中的 Oracle 存储过程中获取 TABLE 类型返回类型?
- python - Tkinter GUI 应用程序无法使用 PyInstaller 在 OSX 中打开
- javascript - Javascript 简单匹配缺少一个值
- for-loop - 为什么 1 在 For 循环中?