C# WPF RichTextBox 将文本格式限制为可见文本
我有一个 RichTextBox,显示一个很大的 FlowDocument(>10k 行)。我正在尝试将文本格式应用于整个文档。这需要一段时间才能完成。
有什么方法可以将格式仅集中在文档的可见部分吗?
信息:我正在尝试搜索 RichTextBox 的内容并突出显示所有匹配的匹配项。搜索功能基于 这个一个。我使用以下代码来“突出显示”找到的每个匹配项。
protected void ColorTextRanges(Color color)
{
foreach ( var textRange in locatedInstances )
{
if ( textRange != null )
{
textRange.ApplyPropertyValue( TextElement.BackgroundProperty, new SolidColorBrush( color ) );
}
}
}
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 ) );
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要在循环中创建画笔,而是在外部创建画笔并重复使用它。虽然不会很重要,但应该会有所帮助。您可以测试BackgroundProperty,并且仅在错误时才设置它 - 这可能会使速度变慢,但如果文档的大部分已经是正确的颜色,那么它应该会有所帮助。
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.
我发现最好的性能提升是在屏幕上未显示输出时更新文档。不确定为什么会这样,但我可以猜测屏幕缓冲区中的某些内容没有被更新。
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.