简体   繁体   English

C#WPF RichTextBox将文本格式限制为可见文本

[英]C# WPF RichTextBox restrict text formatting to visible text

I have a RichTextBox displaying a FlowDocument that is large (>10k lines). 我有一个RichTextBox,显示的FlowDocument很大(> 10k行)。 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. 有关信息:我正在尝试搜索RichTextBox的内容并突出显示所有匹配的事件。 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. 并且您可能会测试BackgroundProperty并仅在错误的情况下进行设置-这可能会使速度变慢,但是如果大多数文档已经是正确的颜色,那么它将有所帮助。

    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. 不确定为什么会这样,但是我可以猜测屏幕缓冲区中的某些内容没有被更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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