Question

I have a descendent of FrameworkElement with the following methods:

class HexVisual : FrameworkElement {

    /* ... */

    private PathGeometry GetHexGeometry(Hexagon Hexagon) {
        var geometry = default(PathGeometry);
        if(mHexGeometries.TryGetValue(Hexagon, out geometry)){
            return geometry;
        }

        PathFigure figure = new PathFigure();

        int i = 0;
        foreach (Point point in Hexagon.Polygon.Points) {
            if (i == 0) {
                figure.StartPoint = point;
                i++;
                continue;
            }

            figure.Segments.Add(new LineSegment(point, true));
        }

        geometry = new PathGeometry();
        geometry.Figures.Add(figure);
        geometry.FillRule = FillRule.Nonzero;

        return geometry;

    }

    private DrawingVisual CreateHexVisual() {
        DrawingVisual visual = new DrawingVisual();
        using (DrawingContext context = visual.RenderOpen()) {
            context.DrawGeometry(BorderBrush, BorderPen, mHexGeometry);
        }

        return visual;

    }
}

When I add this visual to the Canvas, I am getting a filled hexagon rather than just the stroke (border). What am I doing wrong here that is causing the geometry to fill?

Was it helpful?

Solution

Not sure if I really understand your question, because you can't "add visuals to a Canvas", but maybe you just set BorderBrush to null or Transparent, or replace

context.DrawGeometry(BorderBrush, BorderPen, mHexGeometry);

with

context.DrawGeometry(null, BorderPen, mHexGeometry);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top