【问题标题】:Silverlight programmatically binding from C# codeSilverlight 从 C# 代码以编程方式绑定
【发布时间】:2010-03-05 11:03:45
【问题描述】:

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

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

最初我尝试仅将 Line 的 X1 属性与第一个 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

【问题讨论】:

    标签: silverlight binding


    【解决方案1】:

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

    这将是一个永久链接 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 控件,用线条连接,请与我联系。

    【讨论】:

      猜你喜欢
      • 2012-07-29
      • 2023-04-01
      • 2012-09-18
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      • 2011-10-22
      • 1970-01-01
      相关资源
      最近更新 更多