WPF TextBlock根据搜索条件突出显示某些部分 [英] WPF TextBlock highlight certain parts based on search condition
问题描述
我有动态添加了内联的TextBlock(基本上是一堆斜体或粗体的Run对象)。
I have TextBlock that has Inlines dynamicly added to it (basically bunch of Run objects that are either italic or bold).
在我的应用程序中,我具有搜索功能。
In my application I have search function.
我希望能够突出显示正在搜索的TextBlock文本。
I want to be able to highlight TextBlock's text that is in being searched for.
通过突出显示我的意思是更改TextBlock文本颜色的某些部分(请记住,它可能一次突出显示几个不同的Run对象)。
By highlighting I mean changing certain parts of TextBlock text's color (keeping in mind that it may highlight several different Run objects at a time).
但是它接缝非常不稳定:(
But it seams very unstable :(
有没有简单的方法可以解决此问题?
Is there easy way to solve this problem?
推荐答案
此问题类似于如何在带有突出显示查询条件的WPF项目控件中显示搜索结果
为回答这个问题,我想出了一种使用IValueConverter的方法。转换器获取一个文本片段,将其格式化为有效的XAML标记,并使用XamlReader将标记实例化为框架对象。
In answer to that question, I came up with an approach that uses an IValueConverter. The converter takes a text snippet, formats it into valid XAML markup, and uses a XamlReader to instantiate the markup into framework objects.
完整的解释很长,因此我已将其发布到我的博客中:在WPF文本块中突出显示查询项
The full explanation is rather long, so I've posted it to my blog: Highlighting Query Terms in a WPF TextBlock
这篇关于WPF TextBlock根据搜索条件突出显示某些部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!