在 canvas 中绘制多个多边形

Drawing multiple polygons in canvas

我有一个 canvas,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 - 我计划用它来清除下一个多边形的多边形点。但这并没有发生。

请提出任何建议。

我认为问题是您传递的是 polygonpoints 而不是 poly.Points 的副本。

将多边形创建更改为

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