WPF按钮中的多行文本

问题描述 投票:59回答:10

如何仅使用C#在WPF按钮上获取多行文本?我见过在XAML中使用<LineBreak/>的例子,但是我的按钮是用C#完全以编程方式创建的。按钮上的数字和标签对应于域模型中的值,因此我认为我不能使用XAML来指定它。

我尝试了下面的天真方法,但它不起作用。

Button b = new Button();
b.Content = "Two\nLines";

要么

b.Content = "Two\r\nLines";

在任何一种情况下,我看到的只是文本的第一行(“两个”)。

c# wpf multiline
10个回答
92
投票

或直接在XAML中:

<Button>
   <TextBlock>Two<LineBreak/>Lines</TextBlock>  
</Button>

0
投票

试试以下代码:

<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>

46
投票

我更喜欢这种方式:

<Button Width="100">
  <TextBlock TextWrapping="Wrap">This is a fairly long button label</TextBlock>
</Button>

它对我有用。


33
投票

答案很简单。只需使用&#xa;来引入换行符,即:

<Button Content="Row 1 Text &#xa; Row 2 Text"/>

18
投票

有几种方法可以通过XAML执行此操作:

  1. 添加带换行符的TextBlock:
<Button>     
    <TextBlock TextAlignment="Center">Line 1<LineBreak/>Line 2</TextBlock>
</Button>
  1. 在文本中添加换行符:

这种方法很简单,但没有办法轻松控制文本的对齐方式:

    <Button Content="Line 1 &#xa; Line 2"/>
  1. 添加文本块并包装文本

一旦按钮大小小于TextBlocks大小,它将简单地将内容分成两行或更多行

<Button>
  <TextBlock TextWrapping="Wrap" HorizontalAlignment="Center">Line 1 Line 2</TextBlock>
</Button>
  1. 使用Button中的StackPanel,并将每行添加为文本块:
<Button>
    <StackPanel>
        <TextBlock Text="Line1" HorizontalAlignment="Center"/>
        <TextBlock Text="Line2" HorizontalAlignment="Center"/>
    </StackPanel>
</Button>
  1. 在按钮中使用网格:
<Button>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
            <TextBlock Text="Line1" HorizontalAlignment="Center"/>
            <TextBlock Text="Line2" HorizontalAlignment="Center"/>
    </Grid>
</Button>
  • 我相信还有更多的存在,这个列表基本上是从大多数到最不喜欢的顺序。

11
投票

这就是我们在这里做的方式,它也允许轻松居中

<Button Height="40" Width="75">
    <StackPanel>
        <TextBlock Text="Line1" HorizontalAlignment="Center"/>
        <TextBlock Text="Line2" HorizontalAlignment="Center"/>
    </StackPanel>
</Button>

8
投票

原来“\ n”工作正常。我的网格具有固定的大小,并且按钮中没有视觉指示可用的文本更多(例如,没有“......”表示截止)。一旦我慷慨地扩展了网格的大小,按钮文本就显示为两行。


5
投票

你试过这个吗?

b.Content = new TextBlock { 
    Text = "Two\lLines", 
    TextWrapping = TextWrapping.Wrap };

如果这不起作用,那么您可以尝试添加StackPanel作为子项并添加两个TextBlock元素。


4
投票

怎么样:

TextBlock textBlock = new TextBlock();
textBlock.Inlines.Add("Two");
textBlock.Inlines.Add(new LineBreak());
textBlock.Inlines.Add("Lines");
Button button = new Button();
button.Content = textBlock;

如果你正在使用C#3,你可以做一点整洁:

Button button = new Button
{
    Content = new TextBlock { Inlines = { "Two", new LineBreak(), "Lines" } }
};

0
投票

我遇到过同样的问题。

我试过了: - button.content =“Line1 \ nLine2”(没用,也许我做错了); - 用新标签替换按钮文本(不允许您居中对齐文本); - 用文本块替换按钮文本(我认为这可以让你居中对齐文本但不包装它);

我已经看到提到使用stackpanels或网格的答案。 我看到答案说不要使用Textbox

即使OP说\n有效,我也不认为这是要走的路,在我看来,你只是粗暴地迫使控件做你想做的事,如果在任何时候你需要改变文本你将不得不去检查文本是否正确包装或\n是否需要在另一个位置。 我发现(对我来说)最好的方法是用文本框替换按钮内容(你可以拖放,不需要乱用XAML)并设置如下所述的属性:IsReadOnly = true ; Focusable = false(可选); 我认为Focusable = false阻止用户选择文本,即使他不能编辑它,我也不希望他甚至选择它(个人品味)。

这将使文本框的行为类似于标签,但其优点是可以使文本居中对齐。

© www.soinside.com 2019 - 2024. All rights reserved.