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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11893962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:55:35  来源:igfitidea点击:

Avalonedit how to programmatically change background of a text

c#.netwpfavalonedittext-editor

提问by l46kok

I want to implement something that programmatically changes the background of the text when provided with a documentline.(Something that looks very similar to a block selection of a text. I'm going to be using this for debug breakpoints of an IDE I'm designing). I don't want to have to use selection as it causes the textbox to scroll.

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

I think I need to make use of DocumentColorizingTransformer but I'm not 100% sure how to go about this.

我想我需要使用 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
                }
            }
        }
    }

currentLine is the portion that will be highlighted.

currentLine 是将突出显示的部分。

The above code does work properly.. only problem is if the currentLine ever changes while I am viewing that line, it doesn't highlight the updated line until I scroll to another portion of the document (hiding the updated line), and come back to the updated line.

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

Also, how do I make the line numbers start from zero?

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

采纳答案by l46kok

I found the answer

我找到了答案

TxtEditCodeViewer.TextArea.TextView.Redraw();

回答by Erdogan Kurtur

Since it was their creation, I peeked at SharpDevelop's source and how they did it.

由于这是他们的创作,我偷看了 SharpDevelop 的来源以及他们是如何做到的。

They defined a bookmark type (BreakpointBookmark) and added bookmark to the line. bookmark itself sets the color of the line in CreateMarkermethod. It is strange that it is not possible to configure colors of the break-point in SharpDevelop.

他们定义了书签类型 ( BreakpointBookmark) 并将书签添加到行中。书签本身在CreateMarker方法中设置行的颜色。很奇怪,在SharpDevelop中无法配置断点的颜色。

Hope it helps.

希望能帮助到你。

    protected override ITextMarker CreateMarker(ITextMarkerService markerService)
    {
        IDocumentLine line = this.Document.GetLine(this.LineNumber);
        ITextMarker marker = markerService.Create(line.Offset, line.Length);
        marker.BackgroundColor = Color.FromRgb(180, 38, 38);
        marker.ForegroundColor = Colors.White;
        return marker;
    }

回答by Louis Somers

Isn't this a duplicate of this question?

这不是一个重复这个问题

However it looks like you should call InvalidateArrange()on the editor or InvalidateVisual()on each changed visual.

但是看起来您应该调用InvalidateArrange()编辑器或InvalidateVisual()每个更改的视觉对象。