【问题标题】:Drawing multiple polygons in canvas在画布中绘制多个多边形
【发布时间】:2017-09-01 21:05:51
【问题描述】:

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

请有任何建议。

【问题讨论】:

  • 到底发生了什么?
  • 下一步肯定是在左键单击时显示当前多边形。您将首先创建一个新的多边形并将其添加到画布中。保留对当前多边形的引用作为类成员(而不是 PointCollection)。在每次左键单击时将一个点添加到其 Points 属性。右键单击,创建并添加一个新的多边形并对其进行操作。

标签: wpf canvas shapes


【解决方案1】:

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

将多边形创建更改为

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

【讨论】:

  • 请注意,PointCollection 也是 ICollection<Point>,因此无需更改该声明(除非您解释任何可能的优势)。
  • 就是这样......谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
  • 2017-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-09
相关资源
最近更新 更多