0

我正在开发一个控件来绘制一些图表。为此,我想从中派生一个控件PictureBox并向其添加一个字段,该字段基本上是以下简单类的列表:

public class Curve
{
     public List<PointF> DataPoints;
     public Color CurveColor;
     //and constructor and stuff
}

和 PictureBox 类:

public class Graph : PictureBox
{
    List<Curve> Curves;
    //And some code to take care of drawing the curves
}

所以我真的很喜欢这里的一些技巧,有没有办法连接一个事件,所以每当Curve在类中添加或删除一个对象时Graph,它会用剩余的曲线对象重新绘制自己?

我在这里找到了一些细节,但这是为了ArrayList

4

1 回答 1

5

一种方法是在您的 Graph 类中使用 ObservableCollection,然后侦听其 CollectionChanged 事件以重绘您的 Graph。

public class Graph : PictureBox {
    ObservableCollection<Point> Curves;
    //And some code to take care of drawing the curves

    public Graph() {
      Curves.CollectionChanged += Curves_CollectionChanged;
    }

    void Curves_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
      Redraw();
    }
}
于 2012-07-14T22:59:12.377 回答