如何在RichTextBox中的所有行的自定义位置更改文本的颜色?

时间:2018-07-18 13:09:49

标签: c# wpf colors hex richtextbox

如何更改每行中仅定义位置的颜色? 这将在您手动更改文本时起作用。

Example

我有类似的内容,它仅适用于第一行,而在手动更改文本时无法正常工作。

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);
}

0 个答案:

没有答案