0

I have 2 StackPanel, which can be move around inside a canvas. and I want the line to re-drawn when I drag one of the StackPanel. How do I properly bind the line points(X1,Y1,X2,Y2) between the two StackPanel? This is how I did right now :

Line line = new Line();
line.Stroke = new SolidColorBrush(Colors.Black);
line.StrokeThickness = 1.5;

Binding ogX = new Binding();
ogX.Source = originalStackPanel.TransformToAncestor(qbw).Transform(new Point(0, 0)).X;

Binding ogY = new Binding();
ogY.Source = originaStackPanel.TransformToAncestor(qbw).Transform(new Point(0, 0)).Y;

Binding tgX = new Binding();
tgX.Source = targetStackPanel.TransformToAncestor(qbw).Transform(new Point(0, 0)).X;

Binding tgY = new Binding();
tgY.Source = targetStackPanel.TransformToAncestor(qbw).Transform(new Point(0, 0)).Y;

line.SetBinding(Line.X1Property, ogX);
line.SetBinding(Line.Y1Property, ogY);
line.SetBinding(Line.X2Property, tgX);
line.SetBinding(Line.Y2Property, tgY);

and the line only draw once. It won't change dynamically. So how is the correct way to bind line points properly?

enter image description here

added photos for giving 'clear pictures' of the problem :v. so basically the lines is drawn when i drag one of those black boxes, and drop to another black boxes, and a line is drawn, but the panel can move around, so the lines have to re-drawn.

Ardi Sugiarto
  • 85
  • 1
  • 8
  • Besides that your current approach can't work (because there is no Binding source property with change notification), it is unclear how it is supposed to work. In order to get a useful answer, you should explain the desired behaviour. How exactly should the line connect the two panels? How are the panels positioned inside the Canvas, e.g. by Canvas.Left/Top or by a transform? You should certainly have a view model with properties for all related positions. – Clemens Apr 20 '18 at 08:10
  • okay, actually the stackpanel is inside a user control, and i followed the drag code from here https://stackoverflow.com/questions/1495408/how-to-drag-a-usercontrol-inside-a-canvas. from the code it should be using transform, i assume?. as for how exactly should the line connect the two panel, the user click on of the panels, and drag to the second panel, and the line is drawn. – Ardi Sugiarto Apr 20 '18 at 08:25
  • Sorry, I'm behind a firewall and not able to look at your image. – Clemens Apr 20 '18 at 08:37

0 Answers0