突出显示搜索文本块 [英] Highlight Search TextBlock

查看:117
本文介绍了突出显示搜索文本块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是创建一个具有新的依赖项属性SearchText的自定义TextBlock控件。此属性将包含一个正则表达式。此正则表达式在TextBlock文本中的所有出现都将使用自定义样式(另一个DP)突出显示。

My goal is to create a custom TextBlock control that has a new dependency property, SearchText. This property will contain a regular expression. All occurrences of this regular expression in the text of the TextBlock will be highlighted using a custom style (another DP).

我当前的实现涉及清除以下代码中的所有Inline对象: TextBlock的InlineCollection。然后,我使用未突出显示的文本的运行填充TextBlock并使用应用的样式运行突出显示的文本的运行(此方法不支持直接向TextBlock添加内联,而必须使用TextBlock.TextProperty)。

My current implementation involves clearing all of the Inline objects in the TextBlock's InlineCollection. I then fill the TextBlock with runs for unhighlighted text and runs for highlighted text with the style applied (this method does not support adding inlines directly to the TextBlock, instead TextBlock.TextProperty has to be used).

效果很好,但有时在尝试清除Inlines时会遇到一个奇怪的异常:InvalidOperationException:由于当前无法通过树行走,因此目前无法修改该节点的逻辑子级正在进行。

Works great, but sometimes I get a strange exception when trying to clear the Inlines: InvalidOperationException: "Cannot modify the logical children for this node at this time because a tree walk is in progress."

此问题似乎与一个。我正在修改TextChanged函数中的内联,但是我在使用标记来避免无限递归编辑。

This problem seems to be related to this one. I am modifying the inlines in the TextChanged function, but I'm using a flag to avoid infinite recursive edits.

有人对如何构造此自定义控件有任何想法吗?有一个更好的方法吗?

Any thoughts on how to architect this custom control? Is there a better way to do this? How do I get around this exception?

谢谢!

推荐答案

仍然不确定是否有更好的方法可以完全做到这一点,但是我似乎找到了解决方法。

Still not sure if there's a better way to do this altogether, but I appear to have found a work around.

我正在更新一个函数中的内联/运行

I was updating the inlines/runs in a function that was fired by the change notification for the TextProperty and the SearchTextProperty.

现在,我从Dispatcher.BeginInvoke()调用更改通知中的高亮/更新代码, DispatcherPriority.Normal。

Now I'm firing the highlight/update code from a Dispatcher.BeginInvoke() call in the change notification with DispatcherPriority.Normal.

这篇关于突出显示搜索文本块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆