如何更改每行中仅定义位置的颜色? 这将在您手动更改文本时起作用。
我有类似的内容,它仅适用于第一行,而在手动更改文本时无法正常工作。
TextPointer position = richTextBox1.Document.ContentStart;
while (position != null)
{
if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
TextPointer sEvent = position.GetPositionAtOffset(0, LogicalDirection.Forward);
TextPointer eEvent = position.GetPositionAtOffset(2, LogicalDirection.Backward);
new TextRange(sEvent, eEvent).ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
TextPointer sTime = position.GetPositionAtOffset(5, LogicalDirection.Forward);
TextPointer eTime = position.GetPositionAtOffset(11, LogicalDirection.Backward);
new TextRange(sTime, eTime).ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
TextPointer sLenght = position.GetPositionAtOffset(12, LogicalDirection.Forward);
TextPointer eLenght = position.GetPositionAtOffset(16, LogicalDirection.Backward);
new TextRange(sLenght, eLenght).ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Green);
break;
}
position = position.GetNextContextPosition(LogicalDirection.Forward);
}