views:

407

answers:

1

I have a Canvas and a custom control called BasicShape
After I add two BasicShape controls on the Canvas, I want programatically to connect them with a Line and I want to do this using the Binding class.

I want to connect the Bottom side of first shape with the Top side of the second one.

Initially i tried to connect only the X1 property of the Line with the Canvas.Left attached property of the fisrt BasicShape but this doesn't work. Line X1 property is not updated when I change the Canvas.SetLeft(basicShape1) value

        BasicShape bs1 = canvas.Children[0] as BasicShape;
        BasicShape bs2 = canvas.Children[1] as BasicShape;

        Line line = new Line();
        line.StrokeThickness = 1;
        line.Stroke = new SolidColorBrush(Colors.Red);
        line.X1 = 100;
        line.Y1 = 100;
        line.X2 = 200;
        line.Y2 = 200;
        canvas.Children.Add(line);

        Binding b = new Binding("AnyName");
        b.Source = bs1;
        b.Path = new PropertyPath(Canvas.LeftProperty);
        line.SetBinding(Line.X1Property, b);

I'm trying to create a simple UML diagram like this one alt text

A: 

I just did it other way, without binding

This will be a permanent link http://calciusorin.com/SilverlightDiagrams/

I decided to manually update all lines on shape Location or Size changed

    private void basicShape_BasicShapeLocationSizeChangedEvent(BasicShape sender)
    {
        foreach (CustomLine customLine in lines)
        {
            if (customLine.StartFromShape(sender))
            {
                Point point = sender.GetLinePoint(customLine.GetStartSide());
                customLine.SetStartPoint(point);
            }
            if (customLine.EndInShape(sender))
            {
                Point point = sender.GetLinePoint(customLine.GetEndSide());
                customLine.SetEndPoint(point);
            }
        }
    }

I am sure that the Binding solution is more elegant. Anyone interested in my solution, with SL Controls that can be resized, connected with lines, just contact me.

pixel3cs