I have complicated problem (maybe with simple answer).
I have class, that contains some lines, points and "Markers".
Marker
is a class that contains Ellipse
and its center coordinates (Point
).
Marker
class has drag-drop implementation, that moves ellipse and changes Marker.coordinates
property. That works.
However I want to use drag-drop from my Marker
class to move points in SomeShape object (Marker
objects are parts of SomeShape
).
I thought, that when I create Marker
object and I pass 'SomeShape.lineEnds[0]' to Marker
constructor - update on Marker class will update also my SomeShape.lineEnds[0]
, but Its not working.
How can I solve this? By using some references somehow?
I hope I described my problem clearly enough.
Code:
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();
}
}
Problematic part:
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
}
Point is a struct, not class and it will not work like this.
I assume, because structs are not "reference types".
I created own "MyPoint" class with X and Y properties and it worked.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다