简体   繁体   中英

WPF ScrollViewer Slow To Load With RichTextBox

I have following simple code, I just want to load a text a file (file with 82KB and 4000 Lines) into a RichTextBox. But whenever i use ScrollViewer it takes considerable time to load, i want to use a ScrollViewer since i want to scroll to a specific position in the document and scroll two RichTextBoxes synchronously(together). Any Solution for this?

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            string text = System.IO.File.ReadAllText(@"C:\Users\sss\Desktop\WriteLines.txt");
            Paragraph p = new Paragraph();
            Run run = new Run(text) { Foreground = new SolidColorBrush(Color.FromRgb(75, 74, 77)) };
            p.Inlines.Add(run);
            richTextBox1.Document.Blocks.Add(p);
        }
    }
}

//Slow

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ScrollViewer Grid.Row="1"  Name="scrolBase" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <RichTextBox HorizontalAlignment="Left" Name="richTextBox1" VerticalAlignment="Top"   IsReadOnly="True" ScrollViewer.VerticalScrollBarVisibility="Auto"
                         Width="500" 
                         ScrollViewer.HorizontalScrollBarVisibility ="Auto"
                         >
                <FlowDocument PageWidth="1000" />
            </RichTextBox>
        </ScrollViewer>
    </Grid>
</Window>

//Fast

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

            <RichTextBox HorizontalAlignment="Left" Name="richTextBox1" VerticalAlignment="Top"   IsReadOnly="True" ScrollViewer.VerticalScrollBarVisibility="Auto"
                         Width="500" 
                         ScrollViewer.HorizontalScrollBarVisibility ="Auto"
                         >
                <FlowDocument PageWidth="1000" />
            </RichTextBox>

    </Grid>
</Window>

I asked a similar question yesterday,

In WPF RichTextBox takes too long to Load when there is no height

Why not just use the RichTextBox's scrollbar? Set VerticalScrollBarVisibility="Visible"

You can utilize the different Scroll (ScrollToEnd(), ScrollToVerticalOffset(), etc) methods provided by the RichTextBox class.

I know WPF isn't the best at handling large datasets in its controls. Maybe this is caused because the scrollviewer allows for the richtextbox to render itself in its entirety but when the height of the richtextbox is a limited height, it only renders what it needs to? I agree with Michael G - have you tried using the vertical scroll within the richTextBox? This may be better tested for large amounts of data...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM