WPF 文本框的多行

发布于 2024-08-28 17:24:20 字数 552 浏览 7 评论 0原文

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

基本上我正在尝试制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

御弟哥哥 2024-09-04 17:24:21

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

您可能还希望启用 AcceptsTabSpellCheck.IsEnabled

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

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

满天都是小星星 2024-09-04 17:24:21

下面是一个示例 XAML,它将允许 TextBox 接受多行文本并使用自己的滚动条:

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

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"/>
善良天后 2024-09-04 17:24:21

另外,如果像我一样直接在 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.

仙女山的月亮 2024-09-04 17:24:21

相对应的唯一属性

WPF 中与Winforms 属性

TextBox.Multiline = trueWPF 属性:

TextBox.AcceptsReturn = true 

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

所有其他设置,例如 VerticalAlignementWordWrap 等,仅控制 TextBox 在 UI 中的交互方式,但不会影响 Multiline 行为。

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.

抱着落日 2024-09-04 17:24:21

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文