Search code examples
c#wpfrectanglespathgeometry

How to add a rectangle to PathFigure WPF


I was totally tired of adding a Rectangle to a path figure. Below was my tried code, but it does not results correctly as a Rectangle:

PathGeometry geom = new PathGeometry();
Geometry g = new RectangleGeometry(myrectangel);
geom.AddGeometry(g);

PathFigureCollection collection = geom.Figures;
pathfigure = collection[0];

Is there any other way?


Solution

  • You can combined geometries using a GeometryGroup. A GeometryGroup creates a composite geometry from one or more Geometry objects.

    GeometryGroup gg = new GeometryGroup();            
    gg.Children.Add(new RectangleGeometry(new Rect(0, 0, 100, 100)));
    gg.Children.Add(new RectangleGeometry(new Rect(100, 100, 100, 100)));
    gg.Children.Add(new RectangleGeometry(new Rect(200, 200, 100, 100)));
    
    System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
    path.Data = gg;
    path.Fill = Brushes.Blue;