Domanda

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.

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

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.

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.

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

È stato utile?

Soluzione 2

I found the answer

TxtEditCodeViewer.TextArea.TextView.Redraw();

Altri suggerimenti

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

They defined a bookmark type (BreakpointBookmark) and added bookmark to the line. bookmark itself sets the color of the line in CreateMarker method. It is strange that it is not possible to configure colors of the break-point in 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;
    }

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top