{"id":443,"date":"2013-08-13T13:31:21","date_gmt":"2013-08-13T13:31:21","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=443"},"modified":"2021-01-08T15:51:41","modified_gmt":"2021-01-08T15:51:41","slug":"create-diagram-connectors-using-drag-and-drop","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/create-diagram-connectors-using-drag-and-drop\/","title":{"rendered":"Create diagram connectors using drag and drop"},"content":{"rendered":"<p>In this post we show how to create diagram links via drag-and-drop operation from a NodeListView used as a palette. Since the NodeListView control can contain only DiagramNode objects, we will create a custom node shape representing a connector. Once the dummy connector node is created, the NodeCreated event handler will replace it with a DiagramLink. In addition, the handler will find nearby nodes and automatically connect the link to them.<\/p>\n<p>Let&#8217;s start with a new WPF project. Add a reference to mindfusion.diagramming.dll, and add Diagram and NodeListView controls to the window:<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">ScrollViewer<\/span>\n<span id=\"line321\"><\/span>\t<span class=\"attribute-name\">Grid.Column<\/span>=\"<a class=\"attribute-value\">0<\/a>\"\n<span id=\"line322\"><\/span>\t<span class=\"attribute-name\">Focusable<\/span>=\"<a class=\"attribute-value\">False<\/a>\"\n<span id=\"line323\"><\/span>\t<span class=\"attribute-name\">HorizontalScrollBarVisibility<\/span>=\"<a class=\"attribute-value\">Visible<\/a>\"&gt;\n<span id=\"line324\"><\/span>\t\n<span id=\"line325\"><\/span>\t&lt;<span class=\"start-tag\">diag:Diagram<\/span>\n<span id=\"line326\"><\/span>\t\t<span class=\"attribute-name\">x:Name<\/span>=\"<a class=\"attribute-value\">diagram<\/a>\"\n<span id=\"line327\"><\/span>\t\t<span class=\"attribute-name\">AllowDrop<\/span>=\"<a class=\"attribute-value\">True<\/a>\"\n<span id=\"line328\"><\/span>\t\t<span class=\"attribute-name\">NodeCreated<\/span>=\"<a class=\"attribute-value\">OnNodeCreated<\/a>\"\n<span id=\"line329\"><\/span>\t\t<span class=\"attribute-name\">NodeModified<\/span>=\"<a class=\"attribute-value\">OnNodeModified<\/a>\"&gt;\n<span id=\"line330\"><\/span>\t&lt;\/<span class=\"end-tag\">diag:Diagram<\/span>&gt;\n<span id=\"line331\"><\/span>\t\n<span id=\"line332\"><\/span>&lt;\/<span class=\"end-tag\">ScrollViewer<\/span>&gt;\n<span id=\"line333\"><\/span>\n<span id=\"line334\"><\/span>&lt;<span class=\"start-tag\">diag:NodeListView<\/span>\n<span id=\"line335\"><\/span>\t<span class=\"attribute-name\">x:Name<\/span>=\"<a class=\"attribute-value\">nodeList<\/a>\"\n<span id=\"line336\"><\/span>\t<span class=\"attribute-name\">Grid.Column<\/span>=\"<a class=\"attribute-value\">1<\/a>\"&gt;\n<span id=\"line337\"><\/span>&lt;\/<span class=\"end-tag\">diag:NodeListView<\/span>&gt;<\/pre>\n<p>In the Window constructor, create a custom node shape that will represent connectors:<\/p>\n<pre>var connectorShape = new Shape(\n\tnull, \/\/ no borders\n\tnew[] \/\/ decorations\n\t{\n\t\tnew LineTemplate(10, 10, 10, 50),\n\t\tnew LineTemplate(10, 50, 90, 50),\n\t\tnew LineTemplate(90, 50, 90, 90)\n\t},\n\tnull,\n\tFillRule.Nonzero, \"Connector\");\n<\/pre>\n<p>Add a few nodes to the NodeListView, along with the dummy connector node:<\/p>\n<pre>var item1 = new ShapeNode { Shape = Shapes.Rectangle };\nNodeListView.SetLabel(item1, \"Activity\");\nnodeList.Items.Add(item1);\n\nvar item2 = new ShapeNode { Shape = Shapes.Decision };\nNodeListView.SetLabel(item2, \"Decision\");\nnodeList.Items.Add(item2);\n\nvar item3 = new ShapeNode { Shape = connectorShape };\nNodeListView.SetLabel(item3, \"Connector\");\nnodeList.Items.Add(item3);\n<\/pre>\n<p>In the NodeCreated event handler, check if the newly created node represents a connector, and replace it with a DiagramLink:<\/p>\n<pre>var node = e.Node as ShapeNode;\nif (node != null)\n{\n\tif (node.Shape.Id == \"Connector\")\n\t{\n\t\t\/\/ replace the dummy connector node with a DiagramLink\n\t\tvar bounds = node.Bounds;\n\t\tdiagram.Items.Remove(node);\n\n\t\tvar link = diagram.Factory.CreateDiagramLink(\n\t\t\tbounds.TopLeft, bounds.BottomRight);\n\t\tlink.SegmentCount = 3;\n\t\tlink.Shape = LinkShape.Cascading;\n\n\t\tConnectToNearbyNode(link);\n\t}\n\t...\n}\n<\/pre>\n<p>Otherwise if it is a regular node, set its anchor points and connect it to nearby unconnected links, if there are any:<\/p>\n<pre>else\n{\n\tnode.AnchorPattern = AnchorPattern.Decision2In2Out;\n\tnode.Effects.Add(new GlassEffect());\n\tConnectToNearbyLink(node);\n}\n<\/pre>\n<p>The ConnectToNearbyNode method uses a LINQ query to find nodes in the vicinity of the link&#8217;s start or end point. If one is found at a distance shorter than 120 points, it is set as the link&#8217;s Origin or Destination.<\/p>\n<pre>private void ConnectToNearbyNode(DiagramLink link)\n{\n\t\/\/ connect to a nearby origin node\n\tvar origin = diagram.Nodes.OrderBy(n =&gt;\n\t\tUtilities.Distance(n.GetCenter(), link.StartPoint)).FirstOrDefault();\n\n\tif (origin != null)\n\t{\n\t\tvar distance = Utilities.Distance(origin.GetCenter(), link.StartPoint);\n\t\tif (distance &lt; 120)\n\t\t{\n\t\t\tlink.Origin = origin;\n\t\t\tlink.Route();\n\t\t}\n\t}\n\n\t\/\/ connect to a nearby destination node\n\tvar destination = diagram.Nodes.Where(n =&gt; n != origin).OrderBy(n =&gt;\n\t\tUtilities.Distance(n.GetCenter(), link.EndPoint)).FirstOrDefault();\n\n\tif (destination != null)\n\t{\n\t\tvar distance = Utilities.Distance(destination.GetCenter(), link.EndPoint);\n\t\tif (distance &lt; 120)\n\t\t{\n\t\t\tlink.Destination = destination;\n\t\t\tlink.Route();\n\t\t}\n\t}\n}\n<\/pre>\n<p>Similarly, the ConnectToNearbyLink method finds a nearby unconnected link and sets the specified node as the link&#8217;s Origin or Destination.<\/p>\n<pre>private void ConnectToNearbyLink(DiagramNode node)\n{\n\tvar outLink = diagram.Links.Where(l =&gt; l.Origin is DummyNode).OrderBy(l =&gt;\n\t\tUtilities.Distance(node.GetCenter(), l.StartPoint)).FirstOrDefault();\n\n\tif (outLink != null)\n\t{\n\t\tvar distance = Utilities.Distance(node.GetCenter(), outLink.StartPoint);\n\t\tif (distance &lt; 90)\n\t\t{\n\t\t\toutLink.Origin = node;\n\t\t\toutLink.Route();\n\t\t\treturn;\n\t\t}\n\t}\n\n\tvar inLink = diagram.Links.Where(l =&gt; l.Destination is DummyNode).OrderBy(l =&gt;\n\t\tUtilities.Distance(node.GetCenter(), l.EndPoint)).FirstOrDefault();\n\n\tif (inLink != null)\n\t{\n\t\tvar distance = Utilities.Distance(node.GetCenter(), inLink.EndPoint);\n\t\tif (distance &lt; 90)\n\t\t{\n\t\t\tinLink.Destination = node;\n\t\t\tinLink.Route();\n\t\t\treturn;\n\t\t}\n\t}\n}\n<\/pre>\n<p>The result of several drag and drop operation is displayed below.<br \/>\n<img decoding=\"async\" src=\"https:\/\/mindfusion.dev\/_samples\/LinkDragDrop.png\" alt=\"\" \/><\/p>\n<p>The complete sample project is available for download here:<br \/>\n<a href=\"https:\/\/mindfusion.dev\/_samples\/LinkDragDrop.zip\">https:\/\/mindfusion.dev\/_samples\/LinkDragDrop.zip<\/a><\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we show how to create diagram links via drag-and-drop operation from a NodeListView used as a palette. Since the NodeListView control can contain only DiagramNode objects, we will create a custom node shape representing a connector. Once &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/create-diagram-connectors-using-drag-and-drop\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[95,74],"tags":[131,132,133,130,58],"class_list":["post-443","post","type-post","status-publish","format-standard","hentry","category-diagramming-2","category-sample-code","tag-connectors","tag-drag","tag-drop","tag-palette","tag-wpf"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-79","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=443"}],"version-history":[{"count":8,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/443\/revisions"}],"predecessor-version":[{"id":2459,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/443\/revisions\/2459"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}