wpf icon indicating copy to clipboard operation
wpf copied to clipboard

Richtextbox and textbox large text performance issues

Open tingjiangcao opened this issue 1 year ago • 10 comments

Description

Richtextbox and textbox set a lot of text, about tens of thousands of words, adjusting width and height lags, content editing lags, is it related to virtualisation.

Reproduction Steps

.

Expected behavior

.

Actual behavior

.

Regression?

No response

Known Workarounds

No response

Impact

No response

Configuration

No response

Other information

No response

tingjiangcao avatar Jun 05 '24 04:06 tingjiangcao

@tingjiangcao Can you please provide a sample repro project ?

himgoyalmicro avatar Jun 05 '24 05:06 himgoyalmicro

@tingjiangcao您能提出示例项目吗?

WpfApp1.zip

tingjiangcao avatar Jun 05 '24 14:06 tingjiangcao

Reason: Every time you change the window or add data, the WPF program will recalculate the position and size of each WPF element and then render it. Because you have too much data, the calculation and rendering time will be too long, resulting in lag.

Attach: After testing, virtualization is even worse

        <TextBox Foreground="#7B7A7A"  TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" FontSize="16" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling">
            Your Text
        </TextBox>

Solution: If you need better performance in TextBox or Richtextbox, please use paging.You can refer to the design pattern of Word and not put all the data in one TextBox. A TextBox is equivalent to a page, which is a fixed size and can store limited data.

hongruiyu avatar Jun 06 '24 08:06 hongruiyu

There is no reason why WPF should stick to a sub-optimally performing controls. Look at Visual Studio that can open and colorize large amounts of text using WPF, so clearly there is a way.

On the other side, the use case for the existing controls might not be in alignment with the suggested scenario, and there would need to be some compelling justification to redesign them. Others have suggested AvalonEdit which might give you a better performance.

You can refer to the design pattern of Word

People should stop suggesting text boxes are good controls to make text editors, they were never designed for that and Word does not use "text boxes". If you are building a text editor, you are designing a specialized software for which generic purpose controls are rarely a good choice.

A TextBox is equivalent to a page

Not sure where that is coming from. If you switch Word to draft or outline review, you don't get "pages" but still all the text is rendered and editable.

miloush avatar Jun 06 '24 08:06 miloush

Thank you for your reply, there might be some different undertanding on the needs of op, I'm still researching.

hongruiyu avatar Jun 06 '24 09:06 hongruiyu

I create a lightweight text editor, but now it is a demo, see https://github.com/lindexi/lindexi_gd/pull/57

lindexi avatar Jun 06 '24 09:06 lindexi

Reason: Every time you change the window or add data, the WPF program will recalculate the position and size of each WPF element and then render it. Because you have too much data, the calculation and rendering time will be too long, resulting in lag.

Attach: After testing, virtualization is even worse

        <TextBox Foreground="#7B7A7A"  TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" FontSize="16" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling">
            Your Text
        </TextBox>

Solution: If you need better performance in TextBox or Richtextbox, please use paging.You can refer to the design pattern of Word and not put all the data in one TextBox. A TextBox is equivalent to a page, which is a fixed size and can store limited data.

I just got into WPF not long ago.

The genesis of this was that I wanted to get an effect similar to

tingjiangcao avatar Jun 06 '24 14:06 tingjiangcao

I had a look at the demo and I think it raises a valid issue, this should perform better.

What the issue does not say is that the textbox is wrapping with VerticalScrollBarVisibility="Auto". You can get much better performance if you set this to Visible.

miloush avatar Jun 07 '24 08:06 miloush

This is something that can be heavily optimized once DWrite interop is in C# (2030 maybe? at current tempo).

There are about 6 times more heap allocations than required that could be removed and would cut the time required to execute this significantly as well.

h3xds1nz avatar May 21 '25 10:05 h3xds1nz

a "normal" business document with length 3946718 (158 lines) takes a few seconds in wpf textblock. setting verticalscrollbarvisibility to visible instead of auto is even slower here.

(loads very fast with DevExpress TextEdit)

wstaelens avatar Sep 12 '25 13:09 wstaelens