0

I am trying to to make a specific visible line (e.g. line152), which is defined in code behind, to become a First visible line on TextView. Also, I would like this line to be highlighted. So far I have the implemented the following solution, without lack:

textEditor.ScrollTo(myLine, 0); // Setting the current line Visible (e.g. line152) in TextView
int firstLine = textEditor.TextArea.TextView.GetDocumentLineByVisualTop(textEditor.TextArea.TextView.ScrollOffset.Y).LineNumber; // This is actual top visible line of current TextView ((e.g. line130) 

textEditor.ScrollTo(firstLine - myLine, 0); //Which is not working

For Highlighting this line I found a Draw() function but not sure how to invoke it:

 public void Draw(TextView textView, DrawingContext drawingContext)
    {
        textView.EnsureVisualLines();
        var line = textEditor.Document.GetLineByOffset(textEditor.CaretOffset);
        var segment = new TextSegment { StartOffset = line.Offset, EndOffset = line.EndOffset };

        foreach (Rect r in BackgroundGeometryBuilder.GetRectsForSegment(textView, segment))
        {
            drawingContext.DrawRoundedRectangle(
                new SolidColorBrush(Color.FromArgb(20, 0xff, 0xff, 0xff)),
                new Pen(new SolidColorBrush(Color.FromArgb(30, 0xff, 0xff, 0xff)), 1),
                new Rect(r.Location, new Size(textView.ActualWidth, r.Height)),
                3, 3
            );
        }
    }
Jim
  • 2,760
  • 8
  • 42
  • 66

1 Answers1

5

For scolling, use:

    double visualTop = textEditor.TextArea.TextView.GetVisualTopByDocumentLine(line);
    textEditor.ScrollToVerticalOffset(visualTop);

For highlighting, create a new class that implements the IBackgroundRenderer interface. Then add an instance of your class to the textEditor.TextArea.TextView.BackgroundRenderers collection.

Daniel
  • 15,944
  • 2
  • 54
  • 60
  • Hi, I am facing two problems. One, if my line is close to end of document, it won't scroll up. Two, while I was able to highlight the line by adding a class that implements the IBackgroundRenderer interface, however when I change the line, the previous one is still highligted. Do you know How to remove previous highlightning. Thanks – Jim Nov 11 '13 at 12:58
  • 1
    I have tried many variations of this method but I always get some sort of null reference exception on `GetVisualTopByDocumentLine`. I can't see much more specific info but I notice that while the editor has 3k lines, `TextView.allVisualLines` is empty and when I try to look at the value of `TextView.VisualLines` I get a `ICSharpCode.AvalonEdit.Rendering.VisualLinesInvalidException` – zaknotzach Sep 07 '16 at 21:11