Silverlight以编程方式从C#代码绑定

时间:2010-03-05 11:03:45

标签: silverlight binding

我有一个Canvas和一个名为BasicShape的自定义控件 在Canvas上添加两个BasicShape控件之后,我想以编程方式将它们与Line连接,我想使用Binding类来完成它。

我想将第一个形状的底部连接到第二个形状的顶部。

最初我试图仅将Line的X1属性与fisrt BasicShape的Canvas.Left附加属性连接,但这不起作用。更改Canvas.SetLeft(basicShape1)值

时,行X1属性不会更新
        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);

我正在尝试创建一个像这样的简单UML图 alt text http://www.invariant-corp.com/omechron/images/uml_diagram.gif

1 个答案:

答案 0 :(得分:0)

我只是以其他方式做到了,没有绑定

这将是一个永久链接 http://calciusorin.com/SilverlightDiagrams/

我决定手动更新形状位置或尺寸已更改的所有线条

    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);
            }
        }
    }

我确信Binding解决方案更优雅。任何对我的解决方案感兴趣的人,SL控件可以调整大小,与线连接,只需联系我。