开发者

Silverlight programmatically binding from C# code

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;
     开发者_JAVA技巧   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 http://www.invariant-corp.com/omechron/images/uml_diagram.gif


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜