Multiline Text in a WPF Button

Viewed 99785

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.

11 Answers

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>

As an alternative/workaround if you gotta use an external custom button which you can't edit its content easily, you can always use a Grid and overlay the functional part of the button over your visual if it can be static.

<Grid>
  <TextBlock Foreground="LightGray">Click<LineBreak />Here</TextBlock>
  <MyCustomButton BorderBrush="Transparent" IsPressed="{Binding MyBtn.IsPressed, Mode=TwoWay}" />
</Grid>
Related