Question

I have the need for drawing in the same drawing with lines of different color, thickness etc.

I can create two instances of PathGeometry, but I can't set color on them.

I can create two instances of Path, but can't get them displayed in my control.

What am I doing wrong?

Thanks for any comments!

Anders, Denmark.

Code below only displays "collection" in my control, but I thought it could be a starting point for answers...

        var pathFigure1 = new PathFigure(new Point(0, 0),
                                         new List<PathSegment> {new LineSegment(new Point(10, 10), true)}, false);
        var pathFigure2 = new PathFigure(new Point(20, 20),
                                         new List<PathSegment> {new LineSegment(new Point(30, 30), true)}, false);


        var g1 = new PathGeometry(new List<PathFigure> {pathFigure1});
        var g2 = new PathGeometry(new List<PathFigure> {pathFigure2});

        var p1 = new System.Windows.Shapes.Path
                     {
                         Data = g1,
                         Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0))
                     };
        var p2 = new System.Windows.Shapes.Path
                     {
                         Data = g2,
                         Stroke = new SolidColorBrush(Color.FromRgb(170, 87, 170))
                     };

        var content = new Canvas();
        content.Children.Add(p1);
        content.Children.Add(p2);

        Content = content;
Was it helpful?

Solution

You have started on the right approach, a geometry defines a 'shape', so don;t worry that you cannot set its colour. A Path turns the geometry into a visual representation on the screen, so here you can set the color and stroke thickness.

Your problem is at the very last step, you are setting the content property of your control. Typically this property is used to associate some data object with a control, you then supply a template which is its visual representation.

What you need to do is add your paths as children of a panel.

For example, add a Canvas, or a Grid to your control. Then add your two paths to the Grid / Canvas via their Children collection property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top