0

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?

IAbstract
  • 19,551
  • 15
  • 98
  • 146

1 Answers1

1

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);
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Because `HexVisual` is a descendent of `FrameworkElement` I can add the *visual* to `Canvas` with `VisualChildren` being overridden in the descendent. :) – IAbstract Jun 04 '14 at 21:20
  • Thanks. I updated `HexVisual` for constructor params that would do any of the combinations of fill & border, fill only or border only. – IAbstract Jun 04 '14 at 21:47