答案 0 :(得分:4)
我没有足够的时间来获得完整的答案,但我会写一篇简短的答案,因为网上有关于它的零信息:
在执行第2步后,在边距文件中创建的“[yourEditorMarginName] Factory.cs”文件中设置以下行:
[MarginContainer(PredefinedMarginNames.VerticalScrollBar)]
[Order(Before = PredefinedMarginNames.LineNumber)]
返回“[yourEditorMarginName] .cs”文件。确保在构造函数中删除以下行:
this.Height = 20;
this.ClipToBounds = true;
this.Width = 200;
现在你在构造函数中收到了对IWpfTextView的引用,注册了它的OnLayoutChanged事件(或者使用了一些适合你的其他事件):
TextView.LayoutChanged += OnLayoutChanged;
在OnLayoutChanged中,您可以执行以下操作添加矩形装饰:
var rect = new Rectangle();
double bottom;
double firstLineTop;
MapLineToPixels([someLineYouNeedToHighlight], out firstLineTop, out bottom);
SetTop(rect, firstLineTop);
SetLeft(rect, 0);
rect.Height = bottom - firstLineTop;
rect.Width = [yourWidth];
Color color = [your Color];
rect.Fill = new SolidColorBrush(color);
Children.Add(rect);
这是MapLineToPixels():
private void MapLineToPixels(ITextSnapshotLine line, out double top, out double bottom)
{
double mapTop = ScrollBar.Map.GetCoordinateAtBufferPosition(line.Start) - 0.5;
double mapBottom = ScrollBar.Map.GetCoordinateAtBufferPosition(line.End) + 0.5;
top = Math.Round(ScrollBar.GetYCoordinateOfScrollMapPosition(mapTop)) - 2.0;
bottom = Math.Round(ScrollBar.GetYCoordinateOfScrollMapPosition(mapBottom)) + 2.0;
}
是的,ScrollBar
变量可以通过这种方式获得:
public ScrollbarMargin(IWpfTextView textView, IWpfTextViewMargin marginContainer/*, MarginCore marginCore*/)
{
ITextViewMargin scrollBarMargin = marginContainer.GetTextViewMargin(PredefinedMarginNames.VerticalScrollBar);
ScrollBar = (IVerticalScrollBar)scrollBarMargin;