在Adorner上绘图 2023-11-20 07:26 26阅读 0赞 第一种,我们可以使用VisualCollection,它是一种轻量级的绘图方式。那么如何在adorner上使用VisualCollection呢? 首先要重写属性与方法: private VisualCollection visualChildren; protected override int VisualChildrenCount { get { return this.visualChildren.Count; } } protected override Visual GetVisualChild(int index) { return this.visualChildren[index]; } 添加一个子元素Canvas,然后在Canvs上面绘图 private Pen drawingPen; private Canvas adornerCanvas; public ConnectionAdorner(DesignerCanvas designer, Connection connection) : base(designer) { adornerCanvas = new Canvas(); this.visualChildren = new VisualCollection(this); this.visualChildren.Add(adornerCanvas); drawingPen = new Pen(Brushes.LightSlateGray, 1); drawingPen.LineJoin = PenLineJoin.Round; } 第二种就是直接重写OnRender方法,使用DrawingContext绘图 protected override void OnRender(DrawingContext dc) { base.OnRender(dc); dc.DrawGeometry(null, drawingPen, this.pathGeometry); // without a background the OnMouseMove event would not be fired // Alternative: implement a Canvas as a child of this adorner, like // the ConnectionAdorner does. dc.DrawRectangle(Brushes.Transparent, null, new Rect(RenderSize)); }
还没有评论,来说两句吧...