目标:在WPF中,对lucene.net进行全文检索后的结果中包含的关键字进行高亮显示。
检索结果中的关键字高亮显示,在网页中显示是很简单的,lucene中加标签样式就可以了,可是在WPF中就不行了。
我在WPF中用GridControl显示搜索结果列表,GridControl的模板里用TextBlock显示文字。
写了2个方法来使TextBlock中的文字高亮。
1、查找GridControl中的所有TextBlock
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{