2017-09-01 106 views
0

我有一個畫布myCanvas,我想在指定點的位置繪製多個多邊形。在畫布中繪製多個多邊形

PointCollection polygonpoints = new PointCollection(); 

private void myCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
{ 
    //add polygon collection 
    Point p = e.GetPosition(MapGrid); 
    polygonpoints.Add(p); 
} 

private void myCanvas_MouseRightButtonUp(object sender, MouseButtonEventArgs e) 
{ 
    Polygon poly = new Polygon(); 
    poly.Points = polygonpoints; 
    poly.Fill = Brushes.AliceBlue; 
    MapCanvas.Children.Add(poly); 
    polygonpoints.Clear(); // this is making clear the polygon but the pointcollection is remain 
} 

polygonpoints.Clear - 我計劃使用它來清除下一個多邊形的多邊形點。但這沒有發生。

請提出任何建議。

+0

什麼沒有發生什麼呢? – orhtej2

+0

下一步當然是在左鍵單擊時顯示當前的Polygon。您將首先創建一個新的Polygon並將其添加到Canvas。保留對當前Polygon的引用作爲類成員(而不是PointCollection)。在每次左鍵單擊時在其Points項目中添加一個點。點擊右鍵,創建並添加一個新的多邊形並對其進行操作。 – Clemens

回答

0

我相信問題是你通過polygonpoints而不是它的副本poly.Points

更改多邊形創建到

Polygon poly = new Polygon 
{ 
    Points = new PointCollection(polygonpoints), 
    Fill = Brushes.AliceBlue 
}; 
+1

請注意,'PointCollection'也是'ICollection ',所以不需要改變該聲明(除非你解釋了任何可能的優點)。 – Clemens

+0

多數民衆贊成它..謝謝! – srinivas