Я пытаюсь выделить строку в своем 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);
}
}
И вот как это выглядит, когда я пытаюсь выделить вторую строку:
Вы хоть представляете, почему это происходит?
РЕДАКТИРОВАТЬ: Мой текущий подход включает WPF RichTextBox.