pallavi more

pallavi more

  • 1.2k
  • 167
  • 4.3k

two object interlink without connector adorner

Jul 7 2018 3:45 AM
i designed software using wpf c# .but m stuck on how to interconnect two object on canvas without using connector adorner
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Windows;  
  5. using System.Windows.Controls;  
  6. using System.Windows.Documents;  
  7. using System.Windows.Input;  
  8. using System.Windows.Media;  
  9. namespace DiagramDesigner  
  10. {  
  11. public class Connector : Control, INotifyPropertyChanged  
  12. {  
  13. // drag start point, relative to the DesignerCanvas  
  14. private Point? dragStartPoint = null;  
  15. public ConnectorOrientation Orientation { getset; }  
  16. // center position of this Connector relative to the DesignerCanvas  
  17. private Point position;  
  18. public Point Position  
  19. {  
  20. get { return position; }  
  21. set  
  22. {  
  23. if (position != value)  
  24. {  
  25. position = value;  
  26. OnPropertyChanged("Position");  
  27. }  
  28. }  
  29. }  
  30. // the DesignerItem this Connector belongs to;  
  31. // retrieved from DataContext, which is set in the  
  32. // DesignerItem template  
  33. private DesignerItem parentDesignerItem;  
  34. public DesignerItem ParentDesignerItem  
  35. {  
  36. get  
  37. {  
  38. if (parentDesignerItem == null)  
  39. parentDesignerItem = this.DataContext as DesignerItem;  
  40. return parentDesignerItem;  
  41. }  
  42. }  
  43. // keep track of connections that link to this connector  
  44. private List connections;  
  45. public List Connections  
  46. {  
  47. get  
  48. {  
  49. if (connections == null)  
  50. connections = new List();  
  51. return connections;  
  52. }  
  53. }  
  54. public Connector()  
  55. {  
  56. // fired when layout changes  
  57. base.LayoutUpdated += new EventHandler(Connector_LayoutUpdated);  
  58. }  
  59. // when the layout changes we update the position property  
  60. void Connector_LayoutUpdated(object sender, EventArgs e)  
  61. {  
  62. DesignerCanvas designer = GetDesignerCanvas(this);  
  63. if (designer != null)  
  64. {  
  65. //get centre position of this Connector relative to the DesignerCanvas  
  66. this.Position = this.TransformToAncestor(designer).Transform(new Point(this.Width / 2, this.Height / 2));  
  67. }  
  68. }  
  69. protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)  
  70. {  
  71. base.OnMouseLeftButtonDown(e);  
  72. DesignerCanvas canvas = GetDesignerCanvas(this);  
  73. if (canvas != null)  
  74. {  
  75. // position relative to DesignerCanvas  
  76. this.dragStartPoint = new Point?(e.GetPosition(canvas));  
  77. e.Handled = true;  
  78. }  
  79. }  
  80. protected override void OnMouseMove(MouseEventArgs e)  
  81. {  
  82. base.OnMouseMove(e);  
  83. // if mouse button is not pressed we have no drag operation, ...  
  84. if (e.LeftButton != MouseButtonState.Pressed)  
  85. this.dragStartPoint = null;  
  86. // but if mouse button is pressed and start point value is set we do have one  
  87. if (this.dragStartPoint.HasValue)  
  88. {  
  89. // create connection adorner  
  90. DesignerCanvas canvas = GetDesignerCanvas(this);  
  91. if (canvas != null)  
  92. {  
  93. AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(canvas);  
  94. if (adornerLayer != null)  
  95. {  
  96. ConnectorAdorner adorner = new ConnectorAdorner(canvas, this);  
  97. if (adorner != null)  
  98. {  
  99. adornerLayer.Add(adorner);  
  100. e.Handled = true;  
  101. }  
  102. }  
  103. }  
  104. }  
  105. }  
  106. internal ConnectorInfo GetInfo()  
  107. {  
  108. ConnectorInfo info = new ConnectorInfo();  
  109. info.DesignerItemLeft = DesignerCanvas.GetLeft(this.ParentDesignerItem);  
  110. info.DesignerItemTop = DesignerCanvas.GetTop(this.ParentDesignerItem);  
  111. info.DesignerItemSize = new Size(this.ParentDesignerItem.ActualWidth, this.ParentDesignerItem.ActualHeight);  
  112. info.Orientation = this.Orientation;  
  113. info.Position = this.Position;  
  114. return info;  
  115. }  
  116. // iterate through visual tree to get parent DesignerCanvas  
  117. private DesignerCanvas GetDesignerCanvas(DependencyObject element)  
  118. {  
  119. while (element != null && !(element is DesignerCanvas))  
  120. element = VisualTreeHelper.GetParent(element);  
  121. return element as DesignerCanvas;  
  122. }  
  123. #region INotifyPropertyChanged Members  
  124. // we could use DependencyProperties as well to inform others of property changes  
  125. public event PropertyChangedEventHandler PropertyChanged;  
  126. protected void OnPropertyChanged(string name)  
  127. {  
  128. PropertyChangedEventHandler handler = PropertyChanged;  
  129. if (handler != null)  
  130. {  
  131. handler(thisnew PropertyChangedEventArgs(name));  
  132. }  
  133. }  
  134. #endregion  
  135. }  
  136. // provides compact info about a connector; used for the  
  137. // routing algorithm, instead of hand over a full fledged Connector  
  138. internal struct ConnectorInfo  
  139. {  
  140. public double DesignerItemLeft { getset; }  
  141. public double DesignerItemTop { getset; }  
  142. public Size DesignerItemSize { getset; }  
  143. public Point Position { getset; }  
  144. public ConnectorOrientation Orientation { getset; }  
  145. }  
  146. public enum ConnectorOrientation  
  147. {  
  148. None,  
  149. Left,  
  150. Top,  
  151. Right,  
  152. Bottom  
  153. }  
  154. }