在richtextbox wpf中更改特定文本的背景颜色

时间:2017-12-29 13:49:50

标签: c# richtextbox

我正在处理函数,它将改变RichTextBox文本中标签的颜色(等等等等等等等;这将被更改< / i>等等等等)。我尝试制作一些代码,但它只是正确地突出显示第一个标记。第二个标签将在几个字符之前高亮显示。拜托,我怎么能得到这个工作?

实际状态 - 功能仅在第一个标签上正确着色,其他颜色在之前是彩色字符。

我想要的状态 - 在RichTextBox中只有彩色标签及其内容。

问题部分:

        TextPointer text = txbPlainText.Document.ContentStart;
        while (text.GetPointerContext(LogicalDirection.Forward) != TextPointerContext.Text)
        {
          text = text.GetNextContextPosition(LogicalDirection.Forward);
        }
        TextPointer start = text.GetPositionAtOffset(tagStartIndex);
        TextPointer end = text.GetPositionAtOffset(i + tagBuilder.Length);

        TextRange textRange = new TextRange(start, end);
        textRange.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Color.FromRgb(220, 204, 163)));

它看起来如何: Image with problem

这是完整的功能:

private void ColorizeTags()
{
  string tagString = string.Empty;
  int tagStartIndex = 0;
  char[] txbChars = GetTxbText().ToCharArray();

  for (int i = 0; i < txbChars.Count(); i++)
  {
    char actualChar = txbChars[i];

    if (actualChar == '<' && txbChars[i + 1] != '/')
    {
      StringBuilder tagBuilder = new StringBuilder();
      foreach (string tag in TagList)
      {
        for (int x = i; x < (i + tag.Length); x++)
        {
          if (x > txbChars.Count())
          {
            break;
          }

          tagBuilder.Append(txbChars[x]);
        }

        if (tagBuilder.ToString() == tag)
        {
          tagString = tagBuilder.ToString();
          tagStartIndex = i;
          break;
        }
      }
    }
    else if (actualChar == '<' && txbChars[i + 1] == '/')
    {
      if (string.IsNullOrWhiteSpace(tagString))
      {
        continue;
      }

      string endTag = tagString.Insert(tagString.IndexOf('<') + 1, "/");
      StringBuilder tagBuilder = new StringBuilder();

      for (int c = i; c < (i + endTag.Length); c++)
      {
        tagBuilder.Append(txbChars[c]);
      }

      if (tagBuilder.ToString() == endTag)
      {
        TextPointer text = txbPlainText.Document.ContentStart;
        while (text.GetPointerContext(LogicalDirection.Forward) != TextPointerContext.Text)
        {
          text = text.GetNextContextPosition(LogicalDirection.Forward);
        }
        TextPointer start = text.GetPositionAtOffset(tagStartIndex);
        TextPointer end = text.GetPositionAtOffset(i + tagBuilder.Length);

        TextRange textRange = new TextRange(start, end);
        textRange.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Color.FromRgb(220, 204, 163)));
        tagString = string.Empty;
        continue;
      }
    }
  }
}

private string GetTxbText()
{
  return new TextRange(txbPlainText.Document.ContentStart, txbPlainText.Document.ContentEnd).Text;
}

1 个答案:

答案 0 :(得分:1)

我搜索并发现了问题。问题是当我获得开始和结束的位置偏移时,当前文本指针的值已经改变。

第二个问题是,我的TextPointer text忽略了空格,下面的解决方案。

解决方案:不要直接获得位置偏移。

在:

...
TextPointer text = txbPlainText.Document.ContentStart;
while (text.GetPointerContext(LogicalDirection.Forward) != TextPointerContext.Text)
{
  text = text.GetNextContextPosition(LogicalDirection.Forward);
}
TextPointer start = text.GetPositionAtOffset(tagStartIndex);
TextPointer end = text.GetPositionAtOffset(i + tagBuilder.Length);
...

后:

...
TextPointer text = txbPlainText.Document.ContentStart;
TextPointer start = GetTextPointAt(text, tagStartIndex);
TextPointer end = GetTextPointAt(text, endIndex);
...

private static TextPointer GetTextPointAt(TextPointer from, int pos)
{
  TextPointer ret = from;
  int i = 0;

  while ((i < pos) && (ret != null))
  {
    if ((ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.Text) || (ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.None))
      i++;

    if (ret.GetPositionAtOffset(1, LogicalDirection.Forward) == null)
      return ret;

    ret = ret.GetPositionAtOffset(1, LogicalDirection.Forward);
  }

  return ret;
}

Source of answer