小编kzu*_*zub的帖子

突出显示FlowDocument中的部分文本

我想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);
Run Code Online (Sandbox Code Playgroud)

问题是,我正在搜索文档文本中的索引,但是当我返回时,FlowDocument添加了xaml标记,并且我看到了突出显示的内容.我该如何解决?

c# wpf highlight flowdocument

5
推荐指数
1
解决办法
3687
查看次数

标签 统计

c# ×1

flowdocument ×1

highlight ×1

wpf ×1