问题
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 TextBox
would do.
The WPF TextBox
has a single Text
property which stores a contiguous string. Each time, I want to add content, I need to do textBox.Text += "New Text"
, which is bad.
Ideally, that control would be virtual and require a minimum of resources, just for the visible lines.
I thought about using a standard ListBox
with a VirtualizingStackPanel
, but it does not allow Text Selection across lines.
(At each new line added, I want the control to update)
Any suggestion?
回答1:
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.
The TextBox class has an AppendText() method which should be fast enough for you.
If you need text highlighting / formatting then maybe you want to use RichTextBox.
回答2:
If you have really large content, then unfortunately all the WPF textbox and similar controls are very slow. See this question. You could use AvalonEdit as a replacement.
回答3:
Have you considered or tried the RichTextBox control?
回答4:
A StringBuilder, just append the text to the String builder and instead of doing
textBox.Text += moreText;
do
myStringBuilder.Append(moreText);
textBox.Text = myStringBuilder.ToString();
This should take care of the Schlemiel the Painter's algorithm.
Of course, the string builder should have to be a member of your class so it exists through your object's life span.
来源:https://stackoverflow.com/questions/1413087/wpf-multiline-textbox-for-large-content