Avalonedit 如何以编程方式更改文本的背景

l46*_*kok 5 .net c# wpf text-editor avalonedit

我想实现一些在提供文档行时以编程方式更改文本背景的东西。(看起来与文本块选择非常相似的东西。我将使用它来调试我正在使用的 IDE 的断点设计)。我不想使用选择,因为它会导致文本框滚动。

我想我需要使用 DocumentColorizingTransformer 但我不是 100% 确定如何去做。

public class ColorizeAvalonEdit : ICSharpCode.AvalonEdit.Rendering.DocumentColorizingTransformer
    {
        protected override void ColorizeLine(ICSharpCode.AvalonEdit.Document.DocumentLine line)
        {
            int lineStartOffset = line.Offset;
            string text = CurrentContext.Document.GetText(line);
            int start = 0;
            int index;
            if (line.LineNumber == LogicSimViewCodeWPFCtrl.currentLine)
            {
                while ((index = text.IndexOf(text, start)) >= 0)
                {
                    base.ChangeLinePart(
                        lineStartOffset + index, // startOffset
                        lineStartOffset + index + text.Length, // endOffset
                        (VisualLineElement element) =>
                        {
                            element.TextRunProperties.SetBackgroundBrush(Brushes.Red);

                        });
                    start = index + 1; // search for next occurrence
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

currentLine 是将突出显示的部分。

上面的代码确实工作正常..唯一的问题是如果在我查看该行时 currentLine 发生变化,它不会突出显示更新的行,直到我滚动到文档的另一部分(隐藏更新的行),然后回来到更新的行。

另外,如何使行号从零开始?

l46*_*kok 3

我找到了答案

TxtEditCodeViewer.TextArea.TextView.Redraw();
Run Code Online (Sandbox Code Playgroud)