Sae*_*ani 0 c# generics events list
我正在制作一个控件来绘制一些图形.为此,我想从中派生一个控件PictureBox并向其添加一个字段,它基本上是一个以下简单类的List:
public class Curve
{
public List<PointF> DataPoints;
public Color CurveColor;
//and constructor and stuff
}
Run Code Online (Sandbox Code Playgroud)
而PictureBox类:
public class Graph : PictureBox
{
List<Curve> Curves;
//And some code to take care of drawing the curves
}
Run Code Online (Sandbox Code Playgroud)
所以我真的在这里提供了一些技巧,有没有办法连接一个事件,所以无论何时Curve在Graph类中添加或删除一个对象,它都会用剩余的曲线对象重绘自己?
一种方法是在您的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();
}
}
Run Code Online (Sandbox Code Playgroud)