C# 用于大内容的 WPF 多行文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1413087/
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-08-06 16:16:51  来源:igfitidea点击:

WPF Multiline TextBox for large content

c#wpftextboxmultiline

提问by decasteljau

In a WPF application, I want to build a "Find in Files" output pane, in which I can stream large quantity of text, without re-allocating memory at each line, like the TextBoxwould do.

在 WPF 应用程序中,我想构建一个“在文件中查找”输出窗格,在其中我可以流式传输大量文本,而无需像 那样在每一行重新分配内存TextBox

The WPF TextBoxhas a single Textproperty which stores a contiguous string. Each time, I want to add content, I need to do textBox.Text += "New Text", which is bad.

WPFTextBox有一个Text存储连续字符串的属性。每次,我想添加内容,我需要做textBox.Text += "New Text",这很糟糕。

Ideally, that control would be virtual and require a minimum of resources, just for the visible lines.

理想情况下,该控件将是虚拟的,并且需要最少的资源,仅用于可见线。

I thought about using a standard ListBoxwith a VirtualizingStackPanel, but it does not allow Text Selection across lines.

我想过使用一个标准的ListBoxVirtualizingStackPanel,但它不允许跨行文本选择。

(At each new line added, I want the control to update)

(在添加的每个新行中,我希望控件更新)

Any suggestion?

有什么建议吗?

采纳答案by codymanix

If you do not expect much more than ten-thousands of search results in your application, a TextBlock control or readonly multiline TextBox will suffice by far.

如果您不希望应用程序中的搜索结果超过一万个,那么到目前为止,TextBlock 控件或只读多行 TextBox 就足够了。

The TextBox class has an AppendText() method which should be fast enough for you.

TextBox 类有一个 AppendText() 方法,它对你来说应该足够快了。

If you need text highlighting / formatting then maybe you want to use RichTextBox.

如果您需要文本突出显示/格式设置,那么您可能想使用 RichTextBox。

回答by Drew Noakes

Have you considered or tried the RichTextBoxcontrol?

您是否考虑过或尝试过RichTextBox控件?

回答by Carlo

A StringBuilder, just append the text to the String builder and instead of doing

一个 StringBuilder,只需将文本附加到字符串构建器,而不是做

textBox.Text += moreText;

do

myStringBuilder.Append(moreText);
textBox.Text = myStringBuilder.ToString();

This should take care of the Schlemiel the Painter'salgorithm.

这应该处理Schlemiel 画家的算法。

Of course, the string builder should have to be a member of your class so it exists through your object's life span.

当然,字符串生成器必须是您的类的成员,以便它在您的对象的整个生命周期中都存在。

回答by Daniel Rose

If you have really large content, then unfortunately all the WPF textbox and similar controls are very slow. See this question. You could use AvalonEditas a replacement.

如果您的内容非常大,那么不幸的是,所有 WPF 文本框和类似控件都非常慢。看到这个问题。您可以使用AvalonEdit作为替代。