Avalonedit how to programmatically change text background

I want to implement something that programmatically changes the background of the text when there is a document line. (Something like choosing a block of text. I will use this to debug the IDE breakpoints I design). I do not want to use the selection, as it causes the text field to scroll.

I think I need to use DocumentColorizingTransformer, but I'm not 100% sure how to do 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 part to be highlighted.

The above code is working correctly .. only the problem is that currentLine ever changes while I look at this line, it does not highlight the updated line until I go to another part of the document (hiding the updated line) and go back to the updated line.

Also, how to make line numbers starting from scratch?

+4
source share
3 answers

I found the answer

 TxtEditCodeViewer.TextArea.TextView.Redraw(); 
+2
source

Since this was their creation, I looked at the source of SharpDevelop and how they did it.

They defined the type of bookmarks ( BreakpointBookmark ) and added bookmarks to the line. The bookmark itself sets the line color in the CreateMarker method. It is strange that it is not possible to adjust the colors of the break point in SharpDevelop.

Hope this 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; } 
+3
source

Isn't that a duplicate of this question ?

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

0
source

All Articles