如何在 WPF Viewbox 中保持恒定的 FontSize?
我有一个 Viewbox
,其中包含许多 TextBlock
,这些 TextBlock
由 ViewBox
完美缩放和定位。类似这样:
<Viewbox Stretch="Uniform">
<Canvas Width="100" Height="100">
<Ellipse Width="100" Height="100" Stroke="Black"/>
<TextBlock Width="100" TextAlignment="Center" FontSize="12">Top Center</TextBlock>
</Canvas>
</Viewbox>
如果用户调整 Viewbox
的大小,其内容将完美缩放以匹配。但是,无论 Viewbox
的实际大小如何,我都希望将 FontSize
保留为 12。
我该怎么做?我可以在 XAML 中执行此操作而不附加到 Resize
事件吗?
I have a Viewbox
with a number of TextBlock
s that are scaled and positioned perfectly by the ViewBox
. Something like this:
<Viewbox Stretch="Uniform">
<Canvas Width="100" Height="100">
<Ellipse Width="100" Height="100" Stroke="Black"/>
<TextBlock Width="100" TextAlignment="Center" FontSize="12">Top Center</TextBlock>
</Canvas>
</Viewbox>
If the user resizes the Viewbox
its contents are perfectly scaled to match. However I would like to keep the FontSize
to 12 regardless of the actual size of the Viewbox
.
How can I do this? Can I do this in XAML without attaching to an Resize
event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ViewBox
不允许您保持恒定的字体大小,这不是它的工作原理。您需要将文本放在视图框之外才能发生这种情况:请注意,我从
TextBlock
中删除了 Width 属性,我只是让它拉伸网格的宽度,让文本对齐照顾居中。或者,您可以发挥创意,将
FontSize
属性绑定到ViewBox
的ActualWidth
并使其适当缩放,例如:Converter:
Usage :
ViewBox
won't allow you to keep a constant font size, that's just not how it works. You need to put the text outside the view box for that to happen:Note that I removed the Width property from the
TextBlock
, I just let it stretch for the width of the grid, letting the text alignment take care of the centering.Or, you could get creative and bind the
FontSize
property to theActualWidth
of theViewBox
and having it scaled appropriately, for example:Converter:
Usage:
这也可能是一个简单的修复。
This may be a easy fix too.