Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Poor TextBox performance with wrapping on and large amounts of data

Tags:

c#

wpf

textbox

I have a persistent problem with textboxes in WPF, using .NET 3.5. With a large (5000+ lines) amount of data in a TextBox with wrapping enabled, the window has ridiculously bad performance while being resized.

This only happens with TextWrapping="Wrap". Having the text data bound or just setting it programmatically makes no difference.

The code is literally as simple as this:

<TextBox Margin="12,39,337,29" Text="{Binding Output, Mode=OneWay}" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled" />

The text is bound to a single string. The bound data is not being changed.

Edit: The data is not being changed while the window is resizing, is what I meant. It will be updated in the future. The TextBox does not need to be editable, but the actual text does need to be selectable. /Edit.

I've had a quick play with AvalonEdit, which has the same problem. It seems strange that I can't find any other threads which describe this issue.

Any advice?

Thanks, Rich

like image 667
Rich Avatar asked Dec 28 '25 23:12

Rich


2 Answers

The part that is slow is displaying all that text at once. I have run into this issue before where the TextBox in my control became very large and had a ScrollViewer for the whole control that would handle the very large TextBox.

You are almost doing that, but without setting a max for your height/width, I'm not sure if your ScrollViewer is actually going to be utilized.

My solution is to use the built-in scrollViewer inside of your TextBox(which you are doing), and then limit the size of the textbox height/width so that it isn't trying to render 100% of the text all at once (and actually utilizes the ScrollViewer inside your TextBox)

(FYI, I also like using CanContentScroll=true, though I don't think it will affect the speed)

<TextBox Margin="12,39,337,29" TextWrapping="Wrap" 
         Text="{Binding Output, Mode=OneWay}"
         ScrollViewer.CanContentScroll="true"
         VerticalScrollBarVisibility="auto" 
         HorizontalScrollBarVisibility="auto" 
         MaxHeight="600" MaxWidth="600"/>
like image 61
00jt Avatar answered Dec 31 '25 12:12

00jt


You could react to the windowResizeStart event and disable wrapping for the text block. Then wrap just once when the resize completes.

You gain performance but lose some visual flair I guess.

like image 40
chris Avatar answered Dec 31 '25 12:12

chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!