WPF 自动调整字体大小,直到它适合父控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14881683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:32:02  来源:igfitidea点击:

WPF automatic resize font until it fits within parent control

wpfwpf-controlsfont-size

提问by Frinavale

I have a user control who's root element is a Grid.

我有一个用户控件,谁的根元素是网格。

I also have have a ContentControl that serves as a placeholder for some text that is filled when data is loaded. This control can be moved around within the user control and so it's position can be anywhere.

我还有一个 ContentControl 用作某些文本的占位符,在加载数据时填充这些文本。这个控件可以在用户控件内移动,所以它的位置可以在任何地方。

If the text is too long to fit within the root Grid of the user control, I want to reduce the font size of the ContentControl until the text fits.

如果文本太长而无法容纳在用户控件的根网格中,我想减小 ContentControl 的字体大小,直到文本适合为止。

My problem is that I can't seem to find an event that I can handle to do this process. I tried using the ContentControl.LayoutUpdated event;however, the sender parameter for this always appears to be nothing...which is really not helpful!

我的问题是我似乎找不到我可以处理的事件来执行此过程。我尝试使用 ContentControl.LayoutUpdated 事件;但是,此事件的 sender 参数似乎总是什么都没有......这真的没有帮助!

I am really looking forward to any advise on how to achieve this.

我真的很期待就如何实现这一目标提出任何建议。

Thank you

谢谢

-Frinny

-弗林尼

回答by John Bowen

Wrapping your ContentControlin a Viewboxset to only scale down will do this for you:

将您包装ContentControl在一个Viewbox集合中以仅按比例缩小将为您做到这一点:

<Viewbox StretchDirection="DownOnly" Stretch="Uniform">
    <ContentControl Content="Some Text"/>
</Viewbox>