【问题标题】:Avalonedit how to programmatically change background of a textAvalonedit 如何以编程方式更改文本的背景
【发布时间】:2012-08-07 07:13:34
【问题描述】:

我想实现一些在提供文档行时以编程方式更改文本背景的东西。(看起来非常类似于文本的块选择的东西。我将使用它来调试 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
                }
            }
        }
    }

currentLine 是要突出显示的部分。

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

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

【问题讨论】:

    标签: c# .net wpf avalonedit text-editor


    【解决方案1】:

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

    他们定义了一个书签类型 (BreakpointBookmark) 并将书签添加到该行。 书签本身在CreateMarker 方法中设置线条的颜色。奇怪的是在SharpDevelop中不能配置断点的颜色。

    希望对你有帮助。

        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;
        }
    

    【讨论】:

    • 欣赏答案。然而,在查看了sharpdevelop如何处理这个问题之后,我认为必须在对代码进行大量更改以实现看似简单的功能的基础上添加大量的类、接口听起来并不可行。
    • 在内心深处,SharpDevelop 做的和你做的一样。它创建断点作为书签标记,即 TextSegments。在 TextMarkerService.ColorizeLine 中,它找到自己的标记(段)并对整行着色。您只需要创建一个类 MyMarker:TextSegment 并将其保存在类型变量中。 TextSegmentCollection。在 ColorizeLine 中,从 TextMarkerService.ColorizeLine 复制就完成了。
    • 对于行号,您必须编辑 LineNumberMargin.OnRender 并构建 AvalonEdit。
    • 但问题不在于我不能在文本上设置高亮。问题是,尽管我在应用程序中进行了无效调用,但文本的突出显示部分并未正确无效。无论如何我都会尝试这个解决方案,看看我得到了什么,但老实说,我觉得这是一个矫枉过正
    【解决方案2】:

    我找到了答案

    TxtEditCodeViewer.TextArea.TextView.Redraw();
    

    【讨论】:

      【解决方案3】:

      这不是this question的复制品吗?

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

      【讨论】:

      • 我已经打过电话了。没有运气。更具体地说: TxtEditCodeViewer.TextArea.TextView.InvalidateVisual(); TxtEditCodeViewer.TextArea.TextView.InvalidateArrange(); TxtEditCodeViewer.TextArea.TextView.InvalidateMeasure();
      猜你喜欢
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多