i designed software using wpf c# .but m stuck on how to interconnect two object on canvas without using connector adorner
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- namespace DiagramDesigner
- {
- public class Connector : Control, INotifyPropertyChanged
- {
-
- private Point? dragStartPoint = null;
- public ConnectorOrientation Orientation { get; set; }
-
- private Point position;
- public Point Position
- {
- get { return position; }
- set
- {
- if (position != value)
- {
- position = value;
- OnPropertyChanged("Position");
- }
- }
- }
-
-
-
- private DesignerItem parentDesignerItem;
- public DesignerItem ParentDesignerItem
- {
- get
- {
- if (parentDesignerItem == null)
- parentDesignerItem = this.DataContext as DesignerItem;
- return parentDesignerItem;
- }
- }
-
- private List connections;
- public List Connections
- {
- get
- {
- if (connections == null)
- connections = new List();
- return connections;
- }
- }
- public Connector()
- {
-
- base.LayoutUpdated += new EventHandler(Connector_LayoutUpdated);
- }
-
- void Connector_LayoutUpdated(object sender, EventArgs e)
- {
- DesignerCanvas designer = GetDesignerCanvas(this);
- if (designer != null)
- {
-
- this.Position = this.TransformToAncestor(designer).Transform(new Point(this.Width / 2, this.Height / 2));
- }
- }
- protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
- {
- base.OnMouseLeftButtonDown(e);
- DesignerCanvas canvas = GetDesignerCanvas(this);
- if (canvas != null)
- {
-
- this.dragStartPoint = new Point?(e.GetPosition(canvas));
- e.Handled = true;
- }
- }
- protected override void OnMouseMove(MouseEventArgs e)
- {
- base.OnMouseMove(e);
-
- if (e.LeftButton != MouseButtonState.Pressed)
- this.dragStartPoint = null;
-
- if (this.dragStartPoint.HasValue)
- {
-
- DesignerCanvas canvas = GetDesignerCanvas(this);
- if (canvas != null)
- {
- AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(canvas);
- if (adornerLayer != null)
- {
- ConnectorAdorner adorner = new ConnectorAdorner(canvas, this);
- if (adorner != null)
- {
- adornerLayer.Add(adorner);
- e.Handled = true;
- }
- }
- }
- }
- }
- internal ConnectorInfo GetInfo()
- {
- ConnectorInfo info = new ConnectorInfo();
- info.DesignerItemLeft = DesignerCanvas.GetLeft(this.ParentDesignerItem);
- info.DesignerItemTop = DesignerCanvas.GetTop(this.ParentDesignerItem);
- info.DesignerItemSize = new Size(this.ParentDesignerItem.ActualWidth, this.ParentDesignerItem.ActualHeight);
- info.Orientation = this.Orientation;
- info.Position = this.Position;
- return info;
- }
-
- private DesignerCanvas GetDesignerCanvas(DependencyObject element)
- {
- while (element != null && !(element is DesignerCanvas))
- element = VisualTreeHelper.GetParent(element);
- return element as DesignerCanvas;
- }
- #region INotifyPropertyChanged Members
-
- public event PropertyChangedEventHandler PropertyChanged;
- protected void OnPropertyChanged(string name)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (handler != null)
- {
- handler(this, new PropertyChangedEventArgs(name));
- }
- }
- #endregion
- }
-
-
- internal struct ConnectorInfo
- {
- public double DesignerItemLeft { get; set; }
- public double DesignerItemTop { get; set; }
- public Size DesignerItemSize { get; set; }
- public Point Position { get; set; }
- public ConnectorOrientation Orientation { get; set; }
- }
- public enum ConnectorOrientation
- {
- None,
- Left,
- Top,
- Right,
- Bottom
- }
- }