WPF按钮中的多行文字 [英] Multiline Text in a WPF Button
问题描述
如何仅使用C#在WPF按钮上获取多行文本?我已经看到了在XAML中使用<LineBreak/>
的示例,但是我的按钮是完全在C#中以编程方式创建的.按钮上的数字和标签与域模型中的值相对应,因此我认为无法使用XAML来指定此值.
How do I get multi-line text on a WPF Button using only C#? I have seen examples of using <LineBreak/>
in XAML, but my buttons are created completely programmatically in C#. The number and labels on the buttons correspond to values in the domain model, so I don't think I can use XAML to specify this.
我在下面尝试过幼稚的方法,但是它不起作用.
I have tried the naive approach below, but it does not work.
Button b = new Button();
b.Content = "Two\nLines";
或
b.Content = "Two\r\nLines";
无论哪种情况,我所看到的都是文本的第一行("Two").
In either case, all i see is the first line ("Two") of the text.
推荐答案
原来,"\ n"效果很好.我的网格具有固定的大小,并且按钮中根本没有视觉指示,表明有更多可用文本(例如,没有"..."表示截断).一旦慷慨地扩展了网格的大小,按钮文本就会显示为两行.
Turns out the "\n" works fine. My grid had a fixed size, and there is simply no visual indication in a button that there's more text available (e.g., no "..." indicating a cutoff). Once I generously expanded the size of my grid, the button text showed up in two rows.
这篇关于WPF按钮中的多行文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!