我有一個畫布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 - 我計劃使用它來清除下一個多邊形的多邊形點。但這沒有發生。
請提出任何建議。
什麼沒有發生什麼呢? – orhtej2
下一步當然是在左鍵單擊時顯示當前的Polygon。您將首先創建一個新的Polygon並將其添加到Canvas。保留對當前Polygon的引用作爲類成員(而不是PointCollection)。在每次左鍵單擊時在其Points項目中添加一個點。點擊右鍵,創建並添加一個新的多邊形並對其進行操作。 – Clemens