如何向PathFigure WPF添加一个矩形

时间:2013-08-07 04:27:20

标签: c# wpf rectangles pathgeometry

我完全厌倦了为路径图添加Rectangle。下面是我尝试过的代码,但它不能正确地生成Rectangle

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

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

还有其他办法吗?

1 个答案:

答案 0 :(得分:5)

您可以使用GeometryGroup组合几何。 GeometryGroup从一个或多个Geometry对象创建复合几何体。

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;