2017-12-29 15 views
0

リッチテキストボックスのテキスト内のタグの色を変更する機能に取り組んでいます(< i>これは変更されます</i> blah blah blah)。私はいくつかのコードを作ろうとしましたが、最初のタグだけが正しく強調表示されています。 2番目のタグは前にハイライトされています。どうすればこの作品を手に入れることができますか?リッチテキストボックスwpfの特定のテキストの色を変更する

実際の状態 - 関数は最初のタグのみを正しく色付けし、その他は以前に色付きの文字です。

状態私は欲しいです - 色付きタグとそれは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; 
} 
+0

https://stackoverflow.com/help([最小限の、完全な、検証可能な例]を作成する方法をお読みください/ mcve)を編集し、あなたの投稿に欠けている情報を追加してください:)もしあなたが[how to ask](https://stackoverflow.com/help/how-to-ask)を読んでいないのなら、そうすることをお勧めします:) 2つのガイドに従うことを強くお勧めします私がSOにリンクしているので、これらのガイドラインに沿って投稿に回答する可能性が高くなります。 「WantedState」と「CurrentState」が何であるかを再度確認してください。ようこそStackOverflow –

+0

ごめんなさい。今私はそれがより良いことを願っています。 –

答えて

1

私が検索し、問題を発見しました。問題は、開始点と終了点の位置オフセットを取得したときに、現在のテキストポインタの値が変更されたことです。

第2の問題は、私の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

関連する問題