408

I am developing an app for sending some feedback.

Basically I'm trying to make a TextBox for comments, but I'm used to the WinForms MultiLine=true. I've set MinLines to 3, which is getting there, but preferably I'd like it if the user is able to type wherever in this block - like press enter and do dot points sort of thing. For example:

- Item 1        blah
- Item 2                blahlb lahbvl   d

But at the moment the text all stays on one line.

- Item 1         blah - Item 2                      blahb blahb blah

These comments will then help fill the body of an email which is sent. It may be pointless if I can't easily keep the same formatting when putting this string into the email body string (so that it looks like it does when sent as it does when typed).

Can I achieve what I'm after or do I have to leave it as all text on one line?

Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
baron
  • 11,011
  • 20
  • 54
  • 88

5 Answers5

833

Enable TextWrapping="Wrap" and AcceptsReturn="True" on your TextBox.

You might also wish to enable AcceptsTab and SpellCheck.IsEnabled too.

WonderWorker
  • 8,539
  • 4
  • 63
  • 74
itowlson
  • 73,686
  • 17
  • 161
  • 157
  • 1
    hey @itowlson if I creating multiline textbox with your method its work better but if I want to set textbox text counter `label1.Content = textBox1.Text.Length;` with this line its work but when I press enter in the textbox counter will increase 2 characters. how can I do this task please help me. – Jay Shukla Aug 26 '13 at 16:36
  • 2
    This happens because the newline is two characters (CR/LF). If you want to treat it as a single character, do something like `textBox1.Text.Replace("\r\n", " ").Length`. Be careful though: if this is meant as user feedback because your back end limits the number of characters, you may need to count the CR/LF as two characters if that's how the back end will count it! – itowlson Aug 28 '13 at 00:40
  • I also ask this problem in this Link http://stackoverflow.com/questions/18459908/multiline-issue-wpf-textbox – Jay Shukla Aug 28 '13 at 08:25
  • 1
    Also make sure that VerticalContentAlignment is set to Stretch – eran otzap Dec 11 '14 at 14:00
  • 1
    Also add surrounding ScrollVewer component in order to have scroll bar. – Borko Djurovic Jul 03 '15 at 21:17
36

Also, if, like me, you add controls directly in XAML (not using the editor), you might get frustrated that it won't stretch to the available height, even after setting those two properties.

To make the TextBox stretch, set the Height="Auto".

UPDATE:

In retrospect, I think this must have been necessary thanks to a default style for TextBoxes specifying the height to some standard for the application somewhere in the App resources. It may be worthwhile checking this if this helped you.

Andre Luus
  • 3,692
  • 3
  • 33
  • 46
34

Here is a sample XAML that will allow TextBox to accept multiline text and it uses its own scrollbars:

<TextBox
Height="200"
Width="500"
TextWrapping="Wrap"
AcceptsReturn="True"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto"/>
Pang
  • 9,564
  • 146
  • 81
  • 122
FireFalcon
  • 831
  • 11
  • 23
14

The only property corresponding in WPF to the

Winforms property: TextBox.Multiline = true

is the WPF property:

TextBox.AcceptsReturn = true 

or

<TextBox AcceptsReturn="True" ...... />

All other settings, such as VerticalAlignement, WordWrap etc., only control how the TextBox interacts in the UI but do not affect the Multiline behaviour.

marsh-wiggle
  • 2,508
  • 3
  • 35
  • 52
12

Contrary to @Andre Luus, setting Height="Auto" will not make the TextBox stretch. The solution I found was to set VerticalAlignment="Stretch"

marsh-wiggle
  • 2,508
  • 3
  • 35
  • 52
Elkvis
  • 728
  • 1
  • 5
  • 21
  • 1
    The default value for 'VerticalAlignment' *is* 'Stretch' [refer MSDN](http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.verticalalignment.aspx). And yes, it really worked for me. It might depend on the control you placed the textbox in though, was it something non-standard? – Andre Luus May 23 '13 at 13:55
  • 1
    Another likelihood is that you have a default style for textboxes defined somewhere in the scope of that TextBox that defined a different value for VerticalAlignment. I would check with Snoop. – Andre Luus May 23 '13 at 13:57
  • 1
    If the container is fixed, the height auto will not work. Every parent container to the top must be able to expand. Wrapping in a scrollbar works too. – Lee Louviere Jun 18 '14 at 19:11