0% found this document useful (0 votes)
3 views5 pages

Multiline For WPF TextBox - Stack Overflow

The document discusses how to create a multiline TextBox in WPF for user comments, similar to WinForms' MultiLine feature. Key properties to enable include TextWrapping, AcceptsReturn, and potentially AcceptsTab for better formatting. Several user comments and solutions are provided to address common issues and enhance functionality, such as text counting and scrollbar integration.

Uploaded by

drawdenohj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Multiline For WPF TextBox - Stack Overflow

The document discusses how to create a multiline TextBox in WPF for user comments, similar to WinForms' MultiLine feature. Key properties to enable include TextWrapping, AcceptsReturn, and potentially AcceptsTab for better formatting. Several user comments and solutions are provided to address common issues and enhance functionality, such as text counting and scrollbar integration.

Uploaded by

drawdenohj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2/2/26, 10:01 AM Multiline for WPF TextBox - Stack Overflow

Multiline for WPF TextBox


Asked 15 years, 9 months ago Modified 3 years, 6 months ago Viewed 365k times

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
418
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:

Copy
- Item 1 blah
- Item 2 blahlb lahbvl d

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

Copy
- 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?

wpf textbox multiline

Share Improve this question Follow edited Jan 17, 2012 at 13:08 asked Apr 16, 2010 at 2:08
Samuel Slade baron
8,651 6 36 56 11.3k 21 59 89

5 Answers Sorted by: Highest score (default)

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

You might also wish to enable AcceptsTab and [Link] too.


853
Share Improve this answer Follow edited May 2, 2017 at 7:32 answered Apr 16, 2010 at 2:47

[Link] 1/5
2/2/26, 10:01 AM Multiline for WPF TextBox - Stack Overflow
WonderWorker itowlson
9,212 6 70 75 75k 18 165 159

Sign up to request clarification or add additional context in comments.

5 Comments
Add a comment

Jay Shukla Over a year ago


hey @itowlson if I creating multiline textbox with your method its work better but if I want to set textbox
text counter [Link] = [Link]; 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.

1 Reply

itowlson Over a year ago


This happens because the newline is two characters (CR/LF). If you want to treat it as a single character, do
something like [Link]("\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!

2 Reply

Jay Shukla Over a year ago


I also ask this problem in this Link [Link]/questions/18459908/…

0 Reply

eran otzap Over a year ago


Also make sure that VerticalContentAlignment is set to Stretch

1 Reply

Borko Djurovic Over a year ago


Also add surrounding ScrollVewer component in order to have scroll bar.

1 Reply

Here is a sample XAML that will allow TextBox to accept multiline text and it uses its own
scrollbars:
43
Copy
<TextBox
Height="200"
Width="500"
TextWrapping="Wrap"
AcceptsReturn="True"

[Link] 2/5
2/2/26, 10:01 AM Multiline for WPF TextBox - Stack Overflow
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto"/>

Share Improve this answer Follow edited Dec 11, 2020 at 0:14 answered Dec 28, 2016 at 8:18
Pang FireFalcon
10.2k 146 87 126 931 11 26

Comments
Add a comment

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.
36
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.

Share Improve this answer Follow edited Jul 12, 2017 at 9:51 answered Jan 17, 2012 at 13:05
Andre Luus
3,802 3 38 46

Comments
Add a comment

The only property corresponding in WPF to the

Winforms property: [Link] = true


16
is the WPF property:

Copy
[Link] = true

or

Copy

[Link] 3/5
2/2/26, 10:01 AM Multiline for WPF TextBox - Stack Overflow
<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.

Share Improve this answer Follow edited Jul 26, 2022 at 17:06 answered Jun 22, 2019 at 16:24
marsh-wiggle
2,903 3 44 60

Comments
Add a comment

Contrary to @Andre Luus, setting Height="Auto" will not make the TextBox stretch. The solution
I found was to set VerticalAlignment="Stretch"
12
Share Improve this answer Follow edited Dec 19, 2020 at 10:58 answered May 6, 2013 at 16:15
marsh-wiggle Elkvis
2,903 3 44 60 749 1 5 21

3 Comments
Add a comment

Andre Luus Over a year ago


The default value for 'VerticalAlignment' is 'Stretch' refer MSDN. And yes, it really worked for me. It might
depend on the control you placed the textbox in though, was it something non-standard?

1 Reply

Andre Luus Over a year ago


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.

1 Reply

Lee Louviere Over a year ago


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.

1 Reply

[Link] 4/5
2/2/26, 10:01 AM Multiline for WPF TextBox - Stack Overflow

Start asking to get answers


Find the answer to your question by asking.

Ask question

Explore related questions

wpf textbox multiline

See similar questions with these tags.

[Link] 5/5

You might also like