Multiline Text in a WPF Button
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";
or
b.Content = "Two\r\nLines";
In either case, all i see is the first line ("Two") of the text.
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.
Answer is very simple. Just use 

to introduce line-break, i.e.:
<Button Content="Row 1 Text 
 Row 2 Text"/>
There are several ways to do this via XAML:
- Add a TextBlock with a line-break:
<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"/>
- 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>
- 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>
- 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.