Multiline Text in a WPF Button

偶尔善良 提交于 2019-12-02 17:49:45

OR in XAML directly:

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

I prefer this way:

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

it worked for me.

Hassan Rahman

Answer is very simple. Just use &#xa; to introduce line-break, i.e.:

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

There are several ways to do this via XAML:

  1. Add a TextBlock with a line-break:
<Button>     
    <TextBlock TextAlignment="Center">Line 1<LineBreak/>Line 2</TextBlock>
</Button>
  1. 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 &#xa; Line 2"/>
  1. 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

<Button>
  <TextBlock TextWrapping="Wrap" HorizontalAlignment="Center">Line 1 Line 2</TextBlock>
</Button>
  1. Use can a StackPanel in your Button, and add each line as a Text Block:
<Button>
    <StackPanel>
        <TextBlock Text="Line1" HorizontalAlignment="Center"/>
        <TextBlock Text="Line2" HorizontalAlignment="Center"/>
    </StackPanel>
</Button>
  1. 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>
  • I'm sure many more exist, the list is basically in order from most to least favorite.

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

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.

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:

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

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

I've seen answers mentioning the use of stackpanels or grids.
I've seen answers saying not to use a 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).

This will make the text box behave similar to a label but with the advantage that lets you center align the text.

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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!