Multiline Text in a WPF Button

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.

I prefer this way:

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

it worked for me.


OR in XAML directly:

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

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

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

Tags:

C#

Wpf

Multiline