zaknotzach zaknotzach - 7 months ago 81
C# Question

AvalonEdit scroll to line

I am having a lot of trouble getting an AvalonEdit

to scroll to a specific line. The
behavior is simply to scroll until that line is in the middle of the view. I have tried many different methods found around the internet and SO like getting the offset with

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

But each has had its own issues. For example, in the above line I was getting exceptions with the


I ended up figuring out that, since my text was all uniform height, I could just do line height * line# to get the offset. I first tried to do this using (Editor.TextArea.TextView.DocumentHeight / Editor.Document.LineCount)to calculate the line height but that seemed to be slightly off, probably due to some floating point issues, and was increasingly off the mark the farther you went down in the document. But, again since my document is uniform height, I realized I could use the TextView.DefaultLineHeight property and came up with:

double vertOffset = (Editor.TextArea.TextView.DefaultLineHeight) * Line;

This very consistently scrolls to exactly the right place in the document.