private void AddConnection() { if (mFromShape != null && mToShape != null) { //lets connect them con = new DefaultConnection(); con.StartPoint = new Point(0, 0); con.EndPoint = new Point(300, 150); con.Stroke = Brushes.DimGray; con.Width = 100; con.Height = 100; con.Fill = Brushes.White; con.StrokeThickness = 1; Canvas.Children.Add(con); mFromShape.BindConnection(con, Core.Connection.StartProperty, Core.ConnectorLocation.Bottom); mToShape.BindConnection(con, Core.Connection.EndProperty, Core.ConnectorLocation.Top); con.InvalidateVisual(); Canvas.InvalidateVisual();//really necessary this one?? } }
/// <summary> /// Binds the given connection to the shape at the specified connector location. /// </summary> /// <param name="con">The connection.</param> /// <param name="property">The property of the connection to bind to.</param> /// <param name="location">The location (Right, /// Left, Top or Bottom).</param> public void BindConnection(Connection con, DependencyProperty property, ConnectorLocation location) { //A multibinding is like a many-to-one function which returns a value for a dependency property //in terms of multiple values. //The WidthConverter is the class that does the actual conversion of data to one Point-value. //MultiBinding mb = new MultiBinding(); Binding bb = new Binding("Who"); bb.Source = this; bb.Converter = new ConnectorConverter2(); bb.NotifyOnSourceUpdated = true; bb.Mode = BindingMode.OneWay; bb.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; //mb.Converter = new ConnectorConverter(); //this is nasty hack, don't like it but the connection will not be visible if the //converter does not return because width and/or height is NaN. Argh. //if (double.IsNaN(this.Width)) // this.Width = this.DesiredSize.Width; //if (double.IsNaN(this.Height)) // this.Height = this.DesiredSize.Height; //mb.Mode = BindingMode.TwoWay; //mb.NotifyOnSourceUpdated = true; //Because of the multiple locations we have to switch between the various possibilities. switch (location) { case ConnectorLocation.Top: bb.ConverterParameter = "Top"; break; case ConnectorLocation.Right: bb.ConverterParameter = "Right"; break; case ConnectorLocation.Bottom: bb.ConverterParameter = "Bottom"; break; case ConnectorLocation.Left: bb.ConverterParameter = "Left"; break; default: break; } //Binding bx = new Binding("(Canvas.Left)"); //bx.Source = this; //bx.Mode = BindingMode.TwoWay; //bx.NotifyOnSourceUpdated = true; //mb.Bindings.Add(bx); //Binding by = new Binding("(Canvas.Top)"); //by.Source = this; ////bwh.Mode = BindingMode.TwoWay; //mb.Bindings.Add(by); //Binding bw = new Binding("Width"); //bw.Source = this; ////bww.Mode = BindingMode.TwoWay; //mb.Bindings.Add(bw); //Binding bh = new Binding("Height"); //bh.Source = this; ////bww.Mode = BindingMode.TwoWay; //mb.Bindings.Add(bh); con.SetBinding(property, bb); this.mConnections.Add(con); }