简体   繁体   中英

C# WPF RichTextBox restrict text formatting to visible text

I have a RichTextBox displaying a FlowDocument that is large (>10k lines). I am attempting to apply text formatting to the entire Document. This is taking a while to complete.

Is there any way to focus the formatting on the visible parts of the Document only?

For Information: I am attempting to search through the contents of the RichTextBox and highlight all matching occurrences. The searching function is baised upon this one. I am using the following code to 'highlight' each match found.

protected void ColorTextRanges(Color color)
{
    foreach ( var textRange in locatedInstances )
    {
        if ( textRange != null )
        {
            textRange.ApplyPropertyValue( TextElement.BackgroundProperty, new SolidColorBrush( color ) );
        }
    }
}

Rather than create the brush in the loop create it outside and reuse it. Not going to be major but should help a little. And you might test for the BackgroundProperty and only set it if it is wrong - this might make it slower but if most of the document is already the right color then it should help.

    protected void ColorTextRanges(Color color)
    {
        SolidColorBrush brush = new SolidColorBrush( color );
        foreach ( var textRange in locatedInstances )
        {
            if ( textRange != null )
            {
                textRange.ApplyPropertyValue( TextElement.BackgroundProperty,  brush);
            }
        }
   }

The best performance increase I found was to update the document when out wasn't displayed on the screen. Not sure sure why this is but I can guess that some thing in the screen buffer isn't being updated.

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