19

WPF's RichTextBox control's performance is real slow when a 50KB text document is loaded. Scrolling laggy and pressing Ctrl-A to select all the text takes more than 10 seconds. (This is intantaneous on Notepad).

I'm not doing any fancy bitmap effects. Is this normal? Even typing on an empty RichTextBox seems a bit laggy than normal.

Are there fixes for this? Alternatives? Thanks!

moogs
  • 8,122
  • 8
  • 44
  • 60

4 Answers4

11

You might need to consider using a different text box control.

Daniel Grunwald has written the Wpf text editor for SharpDevelop completely from scratch. It is called AvalonEdit and a good article is on codeproject:

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

It seems that he has done optimizations for large files.

Patrick Klug
  • 14,056
  • 13
  • 71
  • 118
3

I bet Scintilla will outperform most (if not all) free alternatives out there. For WPF, use a WPF wrapper. E.g. ScintillaNET.WPF (haven't tried it though).

Pros

  • Excellent performance.
  • Great number of syntax highlighting schemes.

Cons

  • Don't expect native WPF features to work. E.g. scaling, touch...
l33t
  • 18,692
  • 16
  • 103
  • 180
  • 2
    I tried this in a WPF application for showing large XML files and it's really **great**. Loading and rendering a file of **14 MB** was done in **less than 1 second** (including syntax highlighting). Also the features for code folding and searching are sweet. Well, it's a little bit tricky to figure out the configuration details, but it's good invested time. I used **Nuget** packages: `fernandreu.ScintillaNET` and `fernandreu.ScintillaNET.WPF`. – Beauty May 14 '20 at 15:46
1

AvalonEdit has much better performance and works with large input strings. Here is a minimal example:

MainWindow.xaml:

<Window x:Class="TestProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestProject"
        xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
        mc:Ignorable="d"
        d:DataContext="{d:DesignInstance local:MainWindow}">
    <avalonedit:TextEditor Document="{Binding Document}" />
</Window>

MainWindow.xaml.cs:

using ICSharpCode.AvalonEdit.Document;

namespace TestProject
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            Document.Text = "My string";
        }

        public TextDocument Document { get; } = new TextDocument();
    }
}
Daniel Jonsson
  • 3,261
  • 5
  • 45
  • 66
1

Setting the TextBoxBase-property IsUndoEnabledProperty="False" will reduce the loading time a bit.

If you're using the RichTextBox from Xceed.Wpf.Toolkit you can use the methods BeginInit() and EndInit() for the initial loading of your document.