RichTextBox 内の行を強調表示しようとしています。私の試みは、テキスト内の行の位置を取得してから、を表す TextRange を作成することでしたtext.Substring(offset, word.Length)
。しかし、どういうわけか、RichTextBox は前の行の最後の 2 文字と実際の行の一部の文字または実際の行の一部のみを強調表示します。私の現在のアプローチはこれです:
public void SelectLine(string text)
{
int i = new TextRange(editor.Document.ContentStart, editor.Document.ContentEnd).Text.IndexOf(text);
TextPointer start = editor.Document.ContentStart.GetPositionAtOffset(i);
TextPointer end = start.GetPositionAtOffset(text.Length);
TextRange r = new TextRange(start, end);
if (r != null)
{
r.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Red);
r.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.White);
}
}
2 行目を選択しようとすると、次のようになります。
なぜこれが起こっているのか、何か分かりますか?
編集: 私の現在のアプローチには、WPF RichTextBox が含まれています。