C# WPF 按钮中的多行文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1449276/
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
Multiline Text in a WPF Button
提问by Paul
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.
如何仅使用 C# 在 WPF 按钮上获取多行文本?我看过<LineBreak/>
在 XAML中使用的示例,但我的按钮完全是在 C# 中以编程方式创建的。按钮上的数字和标签对应于域模型中的值,所以我认为我不能使用 XAML 来指定它。
I have tried the naive approach below, but it does not work.
我尝试了下面的幼稚方法,但它不起作用。
Button b = new Button();
b.Content = "Two\nLines";
or
或者
b.Content = "Two\r\nLines";
In either case, all i see is the first line ("Two") of the text.
在任何一种情况下,我看到的都是文本的第一行(“两个”)。
采纳答案by Paul
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.
原来“\n”工作正常。我的网格有一个固定的大小,按钮中根本没有视觉指示有更多可用的文本(例如,没有“...”表示截止)。一旦我慷慨地扩大了我的网格的大小,按钮文本就会显示为两行。
回答by Drew Noakes
Have you tried this?
你试过这个吗?
b.Content = new TextBlock {
Text = "Two\lLines",
TextWrapping = TextWrapping.Wrap };
If that doesn't work, then you could try adding a StackPanel as a child and adding two TextBlock elements to that.
如果这不起作用,那么您可以尝试将 StackPanel 添加为子项并向其添加两个 TextBlock 元素。
回答by Jon Skeet
How about:
怎么样:
TextBlock textBlock = new TextBlock();
textBlock.Inlines.Add("Two");
textBlock.Inlines.Add(new LineBreak());
textBlock.Inlines.Add("Lines");
Button button = new Button();
button.Content = textBlock;
If you're using C# 3 you can make that slightly neater:
如果你使用的是 C# 3,你可以让它稍微整洁一点:
Button button = new Button
{
Content = new TextBlock { Inlines = { "Two", new LineBreak(), "Lines" } }
};
回答by Sergey Malyan
OR in XAML directly:
或直接在 XAML 中:
<Button>
<TextBlock>Two<LineBreak/>Lines</TextBlock>
</Button>
回答by MelloG
I prefer this way:
我更喜欢这种方式:
<Button Width="100">
<TextBlock TextWrapping="Wrap">This is a fairly long button label</TextBlock>
</Button>
it worked for me.
它对我有用。
回答by Dennis O J
This is how we do it here it allows for easy centering as well
这就是我们在这里做的方式,它也允许轻松居中
<Button Height="40" Width="75">
<StackPanel>
<TextBlock Text="Line1" HorizontalAlignment="Center"/>
<TextBlock Text="Line2" HorizontalAlignment="Center"/>
</StackPanel>
</Button>
回答by Hassan Rahman
Answer is very simple. Just use 

to introduce line-break, i.e.:
答案很简单。只是

用来引入换行符,即:
<Button Content="Row 1 Text 
 Row 2 Text"/>
回答by Merav Kochavi
There are several ways to do this via XAML:
有几种方法可以通过 XAML 执行此操作:
- Add a TextBlock with a line-break:
- 添加带有换行符的 TextBlock:
<Button> <TextBlock TextAlignment="Center">Line 1<LineBreak/>Line 2</TextBlock> </Button>
<Button> <TextBlock TextAlignment="Center">Line 1<LineBreak/>Line 2</TextBlock> </Button>
- Add a line-break in the text:
- 在文本中添加换行符:
This method is simple but there is no way to easily control the alignment of the text:
这种方法很简单,但是没有办法轻松控制文本的对齐方式:
<Button Content="Line 1 
 Line 2"/>
<Button Content="Line 1 
 Line 2"/>
- Add a Text Block and Wrap the text
- 添加文本块并换行文本
Once the Buttons size is smaller than the TextBlocks size it will simply split the content into two lines or more automatically
一旦 Buttons 的大小小于 TextBlocks 的大小,它就会自动将内容分成两行或更多行
<Button> <TextBlock TextWrapping="Wrap" HorizontalAlignment="Center">Line 1 Line 2</TextBlock> </Button>
<Button> <TextBlock TextWrapping="Wrap" HorizontalAlignment="Center">Line 1 Line 2</TextBlock> </Button>
- Use can a StackPanel in your Button, and add each line as a Text Block:
- 在您的 Button 中使用一个 StackPanel,并将每一行添加为一个文本块:
<Button> <StackPanel> <TextBlock Text="Line1" HorizontalAlignment="Center"/> <TextBlock Text="Line2" HorizontalAlignment="Center"/> </StackPanel> </Button>
<Button> <StackPanel> <TextBlock Text="Line1" HorizontalAlignment="Center"/> <TextBlock Text="Line2" HorizontalAlignment="Center"/> </StackPanel> </Button>
- Use a Grid in your Button:
- 在按钮中使用网格:
<Button> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <TextBlock Text="Line1" HorizontalAlignment="Center"/> <TextBlock Text="Line2" HorizontalAlignment="Center"/> </Grid> </Button>
<Button> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <TextBlock Text="Line1" HorizontalAlignment="Center"/> <TextBlock Text="Line2" HorizontalAlignment="Center"/> </Grid> </Button>
- I'm sure many more exist, the list is basically in order from most to least favorite.
- 我相信还有更多的存在,列表基本上是从最喜欢到最不喜欢的顺序。
回答by Rui Santos
I had the same issue.
我遇到过同样的问题。
I tried:
- button.content = "Line1\nLine2" (didn't work, maybe I did something wrong);
- replace button text with a new label (doesn't let you center align the text);
- replace button text with a text block (I think this lets you center align the text but doesn't wrap it);
我试过:
- button.content = "Line1\nLine2"(没用,也许我做错了什么);
- 用新标签替换按钮文本(不允许您将文本居中对齐);
- 用文本块替换按钮文本(我认为这可以让您居中对齐文本但不会包装它);
I've seen answers mentioning the use of stackpanels or grids.
I've seen answers saying not to use a Textbox.
我见过提到使用堆栈面板或网格的答案。
我看到答案说不要使用Textbox。
Even though the OP says that the \n
works, I don't think that that is the way to go, in my opinion you're just brute forcing the control to do what you want and if at any point you need to change the text you will have to go and check if the text is correctly wrapped or if the \n
needs to be in another position.
What I found to be the best way to go (to me) is to replace the button content with a text box (you can just drag and drop, no need to mess with XAML) and set the following properties as stated:
IsReadOnly = true;
Focusable = false (optional);
I think Focusable = false
prevents the user from selecting the text, even though he can't edit it, I don't want him to even select it (personal taste).
即使 OP 说\n
可行,我也不认为这是要走的路,在我看来,您只是强行强迫控件执行您想要的操作,并且如果在任何时候您需要更改文本将不得不去检查文本是否正确包装或是否\n
需要在另一个位置。
我发现最好的方法(对我来说)是用文本框替换按钮内容(您只需拖放,无需弄乱 XAML)并按照说明设置以下属性:IsReadOnly = true ;
Focusable = false(可选);
我认为Focusable = false
阻止用户选择文本,即使他无法编辑它,我也不希望他选择它(个人品味)。
This will make the text box behave similar to a label but with the advantage that lets you center align the text.
这将使文本框的行为类似于标签,但具有让您居中对齐文本的优势。
回答by Ghotekar Rahul
Try below code:
试试下面的代码:
<Button Command="{Binding DeliveryDateCommand}" x:Name="cntlbtnf5" Background="White" BorderBrush="#FFABADB3" Grid.Column="4">
<Border BorderBrush="{x:Null}" Height="Auto">
<TextBlock Text="Ctrl + F5 Delivery BillDate" TextWrapping="Wrap" Width="110" TextAlignment="Center" Height="44" />
</Border>
</Button>