我试图利用WPFRichTextEditor执行一个基本合成物。 为此,我要以不同颜色显示{}灰色。
这里应该将RichTextBox的内容分为不同的群体:
List<Tag> SplitIntoParts(TextRange textRange, int level)
{
if (textRange==null||textRange.IsEmpty)
{
return new List<Tag>();
}
string text = textRange.Text;
if (text.Length==0)
{
return new List<Tag>();
}
int startPos=-1, counter = 0;
List<Tag> result=new List<Tag>();
for (int i = 0; i < text.Length; i++)
{
if (text[i]== { )
{
if (counter==0)
{
startPos = i;
}
counter++;
}
if (text[i]== } )
{
if (counter==1)
{
Tag t = new Tag()
{
StartPosition = textRange.Start.GetPositionAtOffset(startPos),
EndPosition = textRange.Start.GetPositionAtOffset(i+1),
Level = level,
Word = text.Substring(startPos,i+1-startPos)
};
result.Add(t);
var tr=new TextRange(textRange.Start.GetPositionAtOffset(startPos + 1), textRange.Start.GetPositionAtOffset(i));
result.AddRange(SplitIntoParts(tr, level + 1));
}
counter--;
}
}
if (counter>0)//some open branches still left
{
var i = text.Length;
Tag t = new Tag()
{
StartPosition = textRange.Start.GetPositionAtOffset(startPos),
EndPosition = textRange.End,
Level = level,
Word = text.Substring(startPos, i - startPos)
};
result.Add(t);
result.AddRange(SplitIntoParts(new TextRange(textRange.Start.GetPositionAtOffset(startPos + 1), textRange.Start.GetPositionAtOffset(i - 1)), level + 1));
}
return result;
}
在这项法典中,我找到了文本Range.Start.GetPositionAtOffset(启动) a) Po:
更不用说,该法典有以下几组:
{test|try}
并用以下代码选出:
var t=new Tag()
{
StartPosition = textRange.Start.GetPositionAtOffset(startPos),
EndPosition = textRange.Start.GetPositionAtOffset(i+1),
Level = level,
Word = text.Substring(startPos,i+1-startPos)
};
(e.g. t.Word== {test|try} )
当我试图以同样的休养方式通过时
var tr=new TextRange(textRange.Start.GetPositionAtOffset(startPos + 1), textRange.Start.GetPositionAtOffset(i));
result.AddRange(SplitIntoParts(tr, level + 1));
而不是“试验”
为什么我忘记了这一行为,我应如何处理这一问题?