77274

Multiline Text in a WPF Button

Question:

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.

Answer1:

OR in XAML directly:

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

Answer2:

I prefer this way:

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

it worked for me.

Answer3:

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

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

Answer4:

There are several ways to do this via XAML:

<ol><li>Add a TextBlock with a line-break:</li> </ol><blockquote> <Button> <TextBlock TextAlignment="Center">Line 1<LineBreak/>Line 2</TextBlock> </Button> </blockquote> <ol start="2"><li>Add a line-break in the text:</li> </ol>

This method is simple but there is no way to easily control the alignment of the text:

<blockquote> <Button Content="Line 1 &#xa; Line 2"/> </blockquote> <ol start="3"><li>Add a Text Block and Wrap the text</li> </ol>

Once the Buttons size is smaller than the TextBlocks size it will simply split the content into two lines or more automatically

<blockquote> <Button> <TextBlock TextWrapping="Wrap" HorizontalAlignment="Center">Line 1 Line 2</TextBlock> </Button> </blockquote> <ol start="4"><li>Use can a StackPanel in your Button, and add each line as a Text Block:</li> </ol><blockquote> <Button> <StackPanel> <TextBlock Text="Line1" HorizontalAlignment="Center"/> <TextBlock Text="Line2" HorizontalAlignment="Center"/> </StackPanel> </Button> </blockquote> <ol start="5"><li>Use a Grid in your Button:</li> </ol><blockquote> <Button> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <TextBlock Text="Line1" HorizontalAlignment="Center"/> <TextBlock Text="Line2" HorizontalAlignment="Center"/> </Grid> </Button> </blockquote> <ul><li>I'm sure many more exist, the list is basically in order from most to least favorite.</li> </ul>

Answer5:

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>

Answer6:

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.

Answer7:

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.

Answer8:

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" } } };

Answer9:

I had the same issue.

I tried:<br /> - button.content = "Line1\nLine2" (didn't work, maybe I did something wrong);<br /> - replace button text with a new label (doesn't let you center align the text);<br /> - 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.<br /> I've seen answers saying not to use a <a href="https://msdn.microsoft.com/en-us/library/system.windows.controls.textbox(v=vs.110).aspx" rel="nofollow">Textbox</a>.

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.<br /> 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;<br /> Focusable = false (optional);<br /> 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).<br />

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

Recommend

  • Table View load more cell?
  • passing data between view controllers programmatically Swift
  • How do I refer a control or view in an item of listview - xamarin forms
  • Toggle menu Button doesn't open sliding menu and shuts down app
  • How does sizeof work for int types?
  • Aligning a Stack pointer 8 byte from 4 byte in ARM assembly
  • How to correctly get a WinForms Button control to draw custom text
  • Jquery UI Sortable, move item automatically
  • Responsive left sidebar open close
  • Prevent page break in text block with iText, XMLWorker
  • apply a javascript function to draggable copy
  • C++ friend class std::vector
  • Plotting densities in R
  • Aptana 3 remove bundle (jquery)
  • netsh acl setting (need alternative method - registry settings?)
  • SyntaxError: (irb):26: both block arg and actual block given
  • Update Google Maps traffic layer without page reloading
  • Installing iPhone App to iPhone
  • Using $this when not in object context
  • Spring security and special characters
  • Nant, Vault & Windows Integrated Authentication
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Bug in WPF DataGrid
  • Perl system calls when running as another user using sudo
  • Trying to switch camera back to front but getting exception
  • jQuery tmpl and DataLink beta
  • How can I estimate amount of memory left with calling System.gc()?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • Free memory of cv::Mat loaded using FileStorage API
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Java static initializers and reflection
  • Cant find why the layout is getting smaller
  • Android Google Maps API OnLocationChanged only called once
  • Programmatically clearing map cache
  • UserPrincipal.Current returns apppool on IIS