如何拉伸方形按钮中的文本?
在我的应用程序中,我有一个方形按钮网格。每个按钮的文本内容是在运行时设置的。在大多数情况下,文本只有一个字符长,但有时会更长。我需要使整个文本始终可见,即拉伸它(更改字体大小)以适合按钮的边框。我该怎么做?
我尝试使用 Viewbox,但没有帮助。
我的 XAML 的简化版本:
<Viewbox Stretch="Uniform">
<Button Content="Text"
Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/>
</Viewbox>
关于如何实现我所需要的(即方形按钮 + 始终适合的文本)有什么想法吗?
In my application, I have a grid of square buttons. The text content for each button is set at runtime. In most cases, the text is just one character long, but sometimes it's longer. I need to make the whole text to be always visible, i.e. stretch it (change the font size) to fit inside the button's border. How do I do it?
I tried to use a Viewbox, but it doesn't help.
A simplified version of my XAML:
<Viewbox Stretch="Uniform">
<Button Content="Text"
Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/>
</Viewbox>
Any ideas on how can I achieve what I need (i.e. square buttons + text that always fits in)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您最初的建议几乎是正确的,请尝试以下操作:
并将其应用于多个按钮:
我的第一个想法是使用 RenderTransform 和转换器。这给出了相同的结果,但更复杂:
和一个转换器
Your original suggestion was nearly correct, try this:
And to apply this to multiple buttons:
My first thought was to use a RenderTransform and a converter. This gives the same result but is more complicated:
and a converter