2013-08-07 176 views
3

我完全厭倦將Rectangle添加到路徑圖中。下面是我試過的代碼,但它正確不結果爲Rectangle如何將矩形添加到PathFigure WPF

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

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

有沒有其他辦法?

回答

5

您可以使用GeometryGroup組合幾何圖形。 A GeometryGroup根據一個或多個對象創建複合幾何。

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; 
+0

感謝Nitesh ......但我需要將它添加到的PathFigure,最後使用它,因爲我有很多rectagles像上面.... /// –

+0

你可以用'GeometryGroup'。 – Nitesh

+0

多數民衆贊成在酷Nitesh,。一個想法...謝謝 –