WPF 文本框的多行
我正在开发一个用于发送一些反馈的应用程序。
基本上我正在尝试制作一个 TextBox
用于注释,但我已经习惯了 WinForms MultiLine=true
。我已将 MinLines
设置为 3,这已经足够了,但如果用户能够在该块中的任何位置键入(例如按 Enter 键并执行点点之类的操作),我最好希望它。例如:
- Item 1 blah
- Item 2 blahlb lahbvl d
但目前文本全部停留在一行上。
- Item 1 blah - Item 2 blahb blahb blah
这些评论将有助于填充发送的电子邮件的正文。如果我在将此字符串放入电子邮件正文字符串时无法轻松保持相同的格式(以便它看起来像发送时的格式和键入时的格式一样),则可能毫无意义。
我可以实现我想要的目标还是必须将其保留为一行上的所有文本?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 TextBox 上启用
TextWrapping="Wrap"
和AcceptsReturn="True"
。您可能还希望启用
AcceptsTab
和SpellCheck.IsEnabled
。Enable
TextWrapping="Wrap"
andAcceptsReturn="True"
on your TextBox.You might also wish to enable
AcceptsTab
andSpellCheck.IsEnabled
too.下面是一个示例
XAML
,它将允许TextBox
接受多行文本并使用自己的滚动条:Here is a sample
XAML
that will allowTextBox
to accept multiline text and it uses its own scrollbars:另外,如果像我一样直接在 XAML 中添加控件(不使用编辑器),您可能会感到沮丧,因为它不会拉伸到可用高度,即使在设置这两个属性之后也是如此。
要使文本框拉伸,请设置
Height="Auto"
。更新:
回想起来,我认为这一定是必要的,因为文本框的默认样式指定了应用程序资源中某个应用程序的某个标准的高度。如果这对您有帮助,可能值得检查一下。
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.
相对应的唯一属性
WPF 中与Winforms 属性
:
TextBox.Multiline = true
是 WPF 属性:或
所有其他设置,例如
VerticalAlignement
、WordWrap
等,仅控制 TextBox 在 UI 中的交互方式,但不会影响Multiline
行为。The only property corresponding in WPF to the
Winforms property:
TextBox.Multiline = true
is the WPF property:
or
All other settings, such as
VerticalAlignement
,WordWrap
etc., only control how the TextBox interacts in the UI but do not affect theMultiline
behaviour.与 @Andre Luus 相反,设置
Height="Auto"
不会使TextBox
拉伸。我找到的解决方案是设置VerticalAlignment="Stretch"
Contrary to @Andre Luus, setting
Height="Auto"
will not make theTextBox
stretch. The solution I found was to setVerticalAlignment="Stretch"