Your VisualHost class would also have to override the VisualChildrenCount property and the GetVisualChild method:
public class VisualHost : FrameworkElement
{
private DrawingVisual path = new DrawingVisual();
public VisualHost()
{
...
AddVisualChild(path);
}
protected override int VisualChildrenCount
{
get { return 1; }
}
protected override Visual GetVisualChild(int index)
{
return path;
}
}
Please note also that it is considered good practice to use IDisposable objects like StreamGeometryContext and DrawingContext in using
statements:
var g = new StreamGeometry();
using (var cr = g.Open())
{
cr.BeginFigure(new Point(0,0), false, false);
...
// no need for cr.Close()
}
using (var crx = path.RenderOpen())
{
var p = new Pen(Brushes.Black, 2);
crx.DrawGeometry(null, p, g);
crx.DrawEllipse(Brushes.Red, p, new Point(50,50), 45, 20);
// no need for crx.Close()
}