我有复杂的问题(也许有简单的答案)。
我有课,其中包含一些线条、点和“标记”。
Marker
是一个包含Ellipse
其中心坐标 ( Point
) 的类。
Marker
类具有拖放实现,可移动椭圆并更改Marker.coordinates
属性。这样可行。
但是,我想使用Marker
类中的拖放来移动 SomeShape 对象中的点(Marker
对象是 的一部分SomeShape
)。
我想,当我创建Marker
对象并将 'SomeShape.lineEnds[0]' 传递给Marker
构造函数时 - Marker 类的更新也会更新我的SomeShape.lineEnds[0]
,但它不起作用。
我该如何解决这个问题?通过以某种方式使用一些参考?
我希望我足够清楚地描述了我的问题。
代码:
class SomeShape
{
// this object is set of lines and "Markers" (class below)
private List<Marker> markers;
private List<Point> lineEnds;
private List<Line> lines;
// my object can redraw itself on canvas
public RedrawMe()
{
// it removes own lines from canvas and it puts new lines
// I call this function after I add points to lineEnds collection etc.
// or when I change coordinates on one of lineEnds (list of points)
}
public void AddPoint(Point p)
{
this.lineEnds.Add(p); // adding point to line ends
this.markers.Add(new Marker(p, this, c)); // adding same point to new marker
RedrawMe();
}
}
有问题的部分:
class Marker
{
public Canvas canvas;
private Ellipse e;
private Point coordinates; // ellipse center coordinates
private Object parent; // I store SomeShape object here to call RedrawMe method on it
public Marker(Point p, Object par, Canvas c)
{
this.coordinates = p;
this.canvas = c;
this.parent = par;
e = MyClassFactory.EllipseForMarker();
e.MouseDown += new System.Windows.Input.MouseButtonEventHandler(e_MouseDown);
e.MouseMove += new System.Windows.Input.MouseEventHandler(e_MouseMove);
e.MouseUp += new System.Windows.Input.MouseButtonEventHandler(e_MouseUp);
c.Children.Add(e);
e.Margin = new Thickness(p.X - (e.Width/2), p.Y - (e.Height/2), 0, 0);
}
public void MoveIt(Point nc) // nc - new coordinates
{
this.e.Margin = new Thickness(nc.X - (e.Width / 2), nc.Y - (e.Height / 2), 0, 0);
this.coordinates.X = nc.X;
this.coordinates.Y = nc.Y;
if (this.parent is SomeShape) ((SomeShape)parent).RedrawMe();
}
#region DragDrop // just drag drop implementation, skip this
private bool is_dragged = false;
void e_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e){
e.Handled = true;
is_dragged = false;
this.e.ReleaseMouseCapture();
}
void e_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) {
if (is_dragged)
{
this.MoveIt(e.GetPosition(canvas));
}
}
void e_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
is_dragged = true;
this.e.CaptureMouse();
}
#endregion // DragDrop
}