在文本FlowDocument的亮点部分 [英] Highlight parts of text in FlowDocument
问题描述
我要强调文本的某些部分在的FlowDocument
根据搜索结果。什么我做的是越来越索引所在的搜索词中的的FlowDocument
文本occures,然后发现指数在适用上的文字范围内启动的背景颜色,在结束。创建索引+搜索词长度
的TextRange内容=新的TextRange(myFlowDocument.ContentStart,
myFlowDocument.ContentEnd);
名单,LT; INT>亮点= GetHighlights(content.Text,搜索);
的foreach(高亮INT指数)
{
变种开始= myFlowDocument.ContentStart;
VAR startPos = start.GetPositionAtOffset(指数);
VAR endPos = start.GetPositionAtOffset(索引+ search.Length);
变种的TextRange =新的TextRange(startPos,endPos);
textRange.ApplyPropertyValue(TextElement.BackgroundProperty,
新的SolidColorBrush(Colors.Yellow));
}
的TextRange newRange =新的TextRange(myFlowDocument.ContentStart,
newDocument.ContentEnd);
的FlowDocument FD =(FlowDocument的)XamlReader.Parse(newRange.Text);
现在的问题是,我正在寻找的索引文档的文本,但是当我回来在的FlowDocument
的XAML标记相加,我看的亮点感动。
我怎样才能解决这个问题?
您需要使用 GetNextContextPosition(LogicalDirection进行迭代。向前)
并获得 TextPointer
,使用这一个与以往 TextPointer
来构建的TextRange
。在此的TextRange
你可以申请你的逻辑。
你不能做的是使用单一的TextRange从的FlowDocument
用于搜索文本。 的FlowDocument
不仅是文本:
私人无效Button_Click(对象发件人, RoutedEventArgs E)
{
字符串搜索= this.content.Text;
TextPointer文本= doc.ContentStart;
,而(真)
{
TextPointer下一= text.GetNextContextPosition(LogicalDirection.Forward);
如果(下一== NULL)
{
中断;
}
的TextRange的txt =新的TextRange(文字,旁边);
INT INDX = txt.Text.IndexOf(搜索);
如果(INDX大于0)
{
TextPointer STA = text.GetPositionAtOffset(INDX);
TextPointer结束= text.GetPositionAtOffset(INDX + search.Length);
的TextRange textR =新的TextRange(STA,结束);
textR.ApplyPropertyValue(TextElement.BackgroundProperty,新的SolidColorBrush(Colors.Yellow));
}
文本=下一个;
}
}
更新:
它不会永诺工作例如,如果你有列表,特殊字符(\t)是由的IndexOf
算的,但 GetPositionAtOffset
没有按' ŧ指望他们:
•列表项1
•列表项2
•列表项3
这行:
INT INDX = txt.Text.IndexOf(搜索);
<$ P $:
可以被替换p> INT INDX = Regex.Replace(txt.Text,
[^ A-ZA-Z0-9 _] +,,RegexOptions.Compiled).IndexOf(搜索) ;
I want to highlight some parts of text in a FlowDocument
based on the search results. What I am doing is getting the indexes where the searched word occures in the text of the FlowDocument
and then apply background color on the text range starting at the found index, ending at the found index + search word length.
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);
The problem is, that I am searching indexes in the text of the document but when I'm returning the FlowDocument
the xaml tags are added and I see the highlights moved.
How can I fix it?
You need to iterate using GetNextContextPosition(LogicalDirection.Forward)
and get TextPointer
, use this one with previous TextPointer
to construct TextRange
. On this TextRange
you can apply your logic.
What you can't do is use single TextRange from FlowDocument
for searching text. FlowDocument
is not only text:
private void Button_Click(object sender, RoutedEventArgs e)
{
String search = this.content.Text;
TextPointer text = doc.ContentStart;
while (true)
{
TextPointer next = text.GetNextContextPosition(LogicalDirection.Forward);
if (next == null)
{
break;
}
TextRange txt = new TextRange(text, next);
int indx = txt.Text.IndexOf(search);
if (indx > 0)
{
TextPointer sta = text.GetPositionAtOffset(indx);
TextPointer end = text.GetPositionAtOffset(indx + search.Length);
TextRange textR = new TextRange(sta, end);
textR.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Yellow));
}
text = next;
}
}
UPDATE:
it does not work allways, for example if you have list, special characters (\t) are counted by IndexOf
, but GetPositionAtOffset
doesn't count them in:
• ListItem 1
• ListItem 2
• ListItem 3
this line:
int indx = txt.Text.IndexOf(search);
could be replaced with:
int indx = Regex.Replace(txt.Text,
"[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled).IndexOf(search);
这篇关于在文本FlowDocument的亮点部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!