WPF 文本框的多行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2650144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:41:14  来源:igfitidea点击:

Multiline for WPF TextBox

wpftextboxmultiline

提问by baron

I am developing an app for sending some feedback.

我正在开发一个应用程序来发送一些反馈。

Basically I'm trying to make a TextBoxfor comments, but I'm used to the WinForms MultiLine=true. I've set MinLinesto 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:

基本上我正在尝试TextBox发表评论,但我已经习惯了 WinForms MultiLine=true。我已经设置MinLines为 3,它正在到达那里,但如果用户能够在此块中的任何位置输入,我最好还是喜欢它 - 比如按 Enter 并做点点之类的事情。例如:

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

我能否实现我所追求的目标,还是必须将所有文本保留在一行中?

回答by itowlson

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

在您的 TextBox 上启用TextWrapping="Wrap"AcceptsReturn="True"

You might also wish to enable AcceptsTaband SpellCheck.IsEnabledtoo.

您可能还希望启用AcceptsTabSpellCheck.IsEnabled

回答by Andre Luus

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.

此外,如果像我一样直接在 XAML 中添加控件(不使用编辑器),即使设置了这两个属性,您也可能会因为它不会拉伸到可用高度而感到沮丧。

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

要使 TextBox 拉伸,请将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.

回想起来,我认为这一定是必要的,因为 TextBox 的默认样式将高度指定为应用程序资源中某处应用程序的某个标准。如果这对您有帮助,可能值得检查一下。

回答by FireFalcon

Here is the sample XAMLthat will allow TextBoxto accept multiline text and it uses it's own Scrollbars:

这是XAML允许TextBox接受多行文本并使用它自己的滚动条的示例:

<TextBox
Height="200"
Width="500"
TextWrapping="Wrap"
AcceptsReturn="True"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto"/>

回答by Elkvis

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

与@Andre Luus 相反,设置Height="Auto"不会TextBox拉伸。我找到的解决方案是设置VerticalAlignment="Stretch"

回答by marsh-wiggle

The only propertycorresponding in WPF to the

唯一属性对应于WPF的

Winformsproperty: TextBox.Multiline = true

Winforms属性:TextBox.Multiline = true

is the WPFproperty: TextBox.AcceptsReturn = true.

WPF属性:TextBox.AcceptsReturn = true

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

All other settings, such as VerticalAlignement,WordWrapetc., only control how the TextBox interacts in the UI but does not affect the Multilinebehaviour.

所有其他设置,如VerticalAlignementWordWrap等,只能控制如何在UI的文本框相互作用,但不影响Multiline行为。