views:

609

answers:

8

What would be a good approach to display and edit large amount of unformatted text (just like notepade does) using WPF? Loading a big string into a TextBox makes the UI unresponsive. The overall performance is not nearly comparable with TextBox Controls of previous Microsoft UI Frameworks.

What options do I have to solve this problem. I do not want to block the UI thread while the text control loads the text. Also I might need some sort of "virtualization" because it might not be a good idea to load the whole text into the control (I guess that 20MB of text would create a lot of glyphs even if they are not visible). It seems that TextBox doesn't even have an AppenText() Method anymore so I don't even have a way to control asynchronous loading of the text.

Isn't this a common problem? It seems that WPF does not provide anything for this out of the box. Why is this so?

+1  A: 

You can just use a textbox with a style that gives the user more room to view the text. There are probably more advanced controls from Telerik and others but if you don't require editing options that should suffice.

Lukasz
The main problem here is perfomance and usability. I do not want to block the UI thread while the text control loads the text also I might need some sort of "virtualization" because it might not be a good idea to load the whole text into the control (20MB of text would create a lot of glyphs).
bitbonk
In that case I would go with a third party control. You can remove the extra formating features and keep it simple like Notepad. A third party control should be able to better handle amounts of text you are talking about. You can also load the text on another thread and than bind it to the control after it is loaded.
Lukasz
A: 

Idea: You could use a background worker or thread to read from the filestream of the text file chunk by chunk and write it to the control's text property and with a little bit of threading knowledge this will be just a peace of cake.

Sorry, I Will Not Provide Code Samples because of my very limited free time.

Edit: If appending text is not an option, then do not use the Text Box, try the FlowDocument and add child elements or TextRanges .

Code Guru
This does not solve the problem that I end up with a lot of unecessary text wich I I think is the cause of the slow text box.
bitbonk
I tried the flowdocument method once... it consumes even more memory because of all the runs that are being added to it
TimothyP
A: 

How about trying something like this:

Keep the whole string in memory but show only a 'slice' of it in the textbox. Size of the that sliced string would be dynamically calculated depending on the size of textbox, font size etc.

Of course this involves a lot of not trivial code for proper displaying, synchronizing and so on, but it seems the way to go.

arconaut
+1  A: 

You could always mix and match technologies: you could drop a WinForms TextBox onto a WPF parent. You lose things like styling, opacity, animation, transforms, etc., but if all that matters is editing text, the WinForms TextBox does that just fine.

Joe White
This is sad, but probably the best option.
bitbonk
+3  A: 

I am not sure if this helps, but have you tried using FlowDocumentPageViewer and FlowDocumentReader?

It also has very good annotations support and looks ideal for loading documents in text format.

Vin
+1  A: 

The problem is that the TextBox is a single container element. List controls, such as ListBox virtualize very well because of container recycling. There really isn't anything simple that you can do to speed up the TextBox.

But the TextBox control does have an AppendText() method:

        TextBox tb = new TextBox();
        tb.AppendText("Hello");

So yes, you can use this to dynamicly add some text just like you mentioned.

McAravey
Cool, I was reading the docs and the inherited members where hidden. Since AppendText() comes from TextBoxBase I didn't see it.
bitbonk
+1  A: 

Have you tried the WPF RichTextBox? You'll definitely want to read up on the FlowDocument information if you go this route.

Lurker Indeed
A: 

AvalonEdit, the text editor in SharpDevelop, was written completely from scratch in WPF and is optimized for large amounts of text. It doesn't support rich text (although it does support syntax highlighting and other cool features such as folding). I think this might fit your bill perfectly.

Here is an article on the editor written by the developer:

http://www.codeproject.com/KB/edit/AvalonEdit.aspx

Patrick Klug
I have too cometo the conclusion that writing your own text control is probably the best solution. It's a bit sad, though.
bitbonk