【发布时间】:2013-09-16 15:06:28
【问题描述】:
我想根据搜索结果突出显示FlowDocument 中的某些文本部分。我正在做的是获取在FlowDocument的文本中出现搜索词的索引,然后在从找到的索引开始的文本范围上应用背景色,以找到的索引+搜索词长度结束。
TextRange content = new TextRange(myFlowDocument.ContentStart,
myFlowDocument.ContentEnd);
List<int> highlights = GetHighlights(content.Text, search);
foreach (int index in highlights)
{
var start = myFlowDocument.ContentStart;
var startPos = start.GetPositionAtOffset(index);
var endPos = start.GetPositionAtOffset(index + search.Length);
var textRange = new TextRange(startPos, endPos);
textRange.ApplyPropertyValue(TextElement.BackgroundProperty,
new SolidColorBrush(Colors.Yellow));
}
TextRange newRange = new TextRange(myFlowDocument.ContentStart,
newDocument.ContentEnd);
FlowDocument fd = (FlowDocument)XamlReader.Parse(newRange.Text);
问题是,我正在搜索文档文本中的索引,但是当我返回 FlowDocument 时,添加了 xaml 标记并且我看到突出显示移动了。
我该如何解决?
【问题讨论】:
标签: c# wpf highlight flowdocument