我试图在后面的代码中创建一个特定的可见行(例如line152),成为TextView上的第一个可见行。另外,我希望突出显示这一行。到目前为止,我已经实现了以下解决方案,不缺:
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
为了突出显示这一行,我找到了一个Draw()函数,但不知道如何调用它:
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
);
}
}
答案 0 :(得分:3)
对于scolling,请使用:
double visualTop = textEditor.TextArea.TextView.GetVisualTopByDocumentLine(line);
textEditor.ScrollToVerticalOffset(visualTop);
要突出显示,请创建一个实现IBackgroundRenderer
接口的新类。然后将您的班级实例添加到textEditor.TextArea.TextView.BackgroundRenderers
集合。