I’m trying to connect a line between two ellipses where if one was dragged the line would move with it. I have a canvas and inside that are two stackpanels… in each stackpanel is an ellipse on the left… content control in the middle… and another ellipse on the right. The idea is to connect a line between the right ellipse from one stackpanel to the left ellipse in the second stackpanel. So far I have this but can’t seem to get much farther as the propertypath being used to do the binding doesn’t make too much sense to me… which is why I have a Canvas in there right now.
Line line = new Line();
line.Stroke = connectedEllipse.Fill;
line.StrokeThickness = 2;
Binding x1 = new Binding();
Binding x2 = new Binding();
Binding y1 = new Binding();
Binding y2 = new Binding();
x1.Path = new PropertyPath(Canvas.LeftProperty);
x2.Path = new PropertyPath(Canvas.LeftProperty);
y1.Path = new PropertyPath(Canvas.TopProperty);
y2.Path = new PropertyPath(Canvas.TopProperty);
x1.Source = y1.Source = connectedEllipse;
x2.Source = y2.Source = (sender as Ellipse);
line.SetBinding(Line.X1Property, x1);
line.SetBinding(Line.X2Property, x2);
line.SetBinding(Line.Y1Property, y1);
line.SetBinding(Line.Y2Property, y2);
Ok I’ve hacked up some code which doesn’t use the attached properties method. This probably isn’t “good” code since I wrote it up in 20 minutes but it will get you started.
MainWindow.xaml
MainWindow.xaml.cs
ExtendedThumb.cs
Also, I got the idea from the contents of this link: http://denisvuyka.wordpress.com/2007/10/13/wpf-draggable-objects-and-simple-shape-connectors/