在画布中绘制多个多边形

时间:2017-09-01 21:05:51

标签: wpf canvas shapes

我有一个画布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 - 我打算用它来清除下一个多边形的多边形点。但这不会发生。

请提出任何建议。

1 个答案:

答案 0 :(得分:0)

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

将多边形创建更改为

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