Multiline for WPF TextBox
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?
Enable TextWrapping="Wrap"
and AcceptsReturn="True"
on your TextBox.
You might also wish to enable AcceptsTab
and SpellCheck.IsEnabled
too.
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.
Contrary to @Andre Luus, setting Height="Auto"
will not make the TextBox
stretch. The solution I found was to set VerticalAlignment="Stretch"
上一篇: 用什么键可以让用户离开WPF文本框?
下一篇: 多行WPF文本框