{"id":1588,"date":"2016-07-08T07:32:11","date_gmt":"2016-07-08T07:32:11","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=1588"},"modified":"2021-01-20T17:43:46","modified_gmt":"2021-01-20T17:43:46","slug":"mindfusion-diagramming-for-wpf-custom-templates","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/mindfusion-diagramming-for-wpf-custom-templates\/","title":{"rendered":"Custom Templates With the WPF Diagram Control"},"content":{"rendered":"<p>In this blog you will learn how to create a presentation of a hierarchical organization using MindFusion WPF Diagram control. The hierarchy is presented as a diagram, where each node represents an employee in the organization and each link represents the direct relationship between employees. The nodes in the diagram use custom templates to give a more detailed description of the employee, as well as to enable editing of various properties.<\/p>\n<p><strong>Setup<\/strong><\/p>\n<p>Create a new <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/aa970268(v=vs.100).aspx\">WPF<\/a> application and add the necessary assemblies to the project. In the main window, declare the following namespace:<\/p>\n<pre>xmlns:diag=\"http:\/\/mindfusion.dev\/diagramming\/wpf\"<\/pre>\n<p>Then declare an instance of the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/wpfdiagram\/index.htm?T_MindFusion_Diagramming_Wpf_DiagramView.htm\">DiagramView<\/a> class inside the root grid:<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">diag:diagramview<\/span> <span class=\"attribute-name\">x:name<\/span>=\"<a class=\"attribute-value\">diagramView<\/a>\"&gt;\n<span id=\"line252\"><\/span>    &lt;<span class=\"start-tag\">diag:diagram<\/span> <span class=\"attribute-name\">x:name<\/span>=\"<a class=\"attribute-value\">diagram<\/a>\" <span class=\"attribute-name\">backbrush<\/span>=\"<a class=\"attribute-value\">White<\/a>\"&gt;\n<span id=\"line253\"><\/span>&lt;\/<span class=\"end-tag\">diag:diagram<\/span>&gt;&lt;\/<span class=\"end-tag\">diag:diagramview<\/span>&gt;<\/pre>\n<p><strong>Creating the custom node<\/strong><\/p>\n<p>To create the custom node, from the &#8220;Project -&gt; Add New Item&#8221; menu add a new CustomControl (WPF) item to the project. This automatically creates a <em>themes<\/em> folder inside the project and a <em>generic.xaml<\/em> resource dictionary, which contains the template of the newly added class. Rename the newly created file (and class) to OrgChartNode. Ensure that the new class derives from <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/wpfdiagram\/index.htm?T_MindFusion_Diagramming_Wpf_TemplatedNode.htm\">TemplatedNode<\/a> rather than Control. Then define the following dependency properties in the class: Title, FullName, and Image, of types string and ImageSource respectively.<\/p>\n<p>Define the appearance of the node in its template in the <em>generic.xaml<\/em> file. In this case the node will display a round border, an image of the employee, its title, name, and description, and several buttons that can be used to change the role of the employee or add subordinates. The components bind directly to the properties of the node class. For example:<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">textbox<\/span> <span class=\"attribute-name\">acceptsreturn<\/span>=\"<a class=\"attribute-value\">False<\/a>\" <span class=\"attribute-name\">fontfamily<\/span>=\"<a class=\"attribute-value\">Verdana<\/a>\" <span class=\"attribute-name\">fontsize<\/span>=\"<a class=\"attribute-value\">12<\/a>\" <span class=\"attribute-name\">borderbrush<\/span>=\"<a class=\"attribute-value\">Transparent<\/a>\" <span class=\"attribute-name\">background<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/mindfusion-diagramming-for-wpf-custom-templates\/Transparent\">Transparent<\/a>\" <span class=\"attribute-name\">text<\/span>=\"<a class=\"attribute-value\">{Binding FullName}<\/a>\"&gt;&lt;\/<span class=\"end-tag\">textbox<\/span>&gt;<\/pre>\n<p>The complete listing of the node&#8217;s template can be found in the project below.<\/p>\n<p>To handle the Click event of the buttons in the template, register a routed event handler in the OrgChartNode class:<\/p>\n<pre>AddHandler(Button.ClickEvent, new RoutedEventHandler(OnClick));\n...\nvoid OnClick(object sender, RoutedEventArgs e)\n{\n    ...\n}<\/pre>\n<p>Declare an Index property in the OrgChartNode class, which will indicate the role of the employee. Changing the role will automatically update the title and background color of the node:<\/p>\n<pre>public int Index\n{\n    get { return Images.IndexOf(Image); }\n    set\n    {\n        if (value != -1)\n        {\n            Image = Images[value];\n            Title = Titles[value];\n            Brush = Fills[value];\n        }\n        else\n        {\n            Image = null;\n        }\n\n        InvalidateVisual();\n    }\n}<\/pre>\n<p><strong>Create the hierarchy<\/strong><\/p>\n<p>Now that the custom node is ready, we can create a diagram representing the hierarchy. In the code behind of the main window, create a series of OrgChartNode objects, each representing an employee in the organization, then link the nodes using the <a title=\"WPF Diagram Control : CreateDiagramLink\" href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/wpfdiagram\/index.htm?M_MindFusion_Diagramming_Wpf_Factory_CreateDiagramLink_2_DiagramNode_DiagramNode.htm\">CreateDiagramLink<\/a> method of the diagram <a title=\"WPF Diagram Control : Factory\" href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/wpfdiagram\/index.htm?T_MindFusion_Diagramming_Wpf_Factory.htm\">Factory<\/a> class:<\/p>\n<pre>var node1 = new OrgChartNode\n{\n    Bounds = new Rect(0, 0, 300, 160),\n    FullName = \"Mike Powell\",\n    Text = \"This is the leader of the sample organization.\",\n    Index = 2\n};\ndiagram.Nodes.Add(node1);\n\nvar node2 = new OrgChartNode\n{\n    Bounds = new Rect(0, 0, 300, 160),\n    FullName = \"Emily Williams\",\n    Text = \"Emily is the leader highest in the PR hierarchy.\",\n    Index = 1\n};\ndiagram.Nodes.Add(node2);\n...\ndiagram.Factory.CreateDiagramLink(node1, node2);<\/pre>\n<p>Finally, arrange the hierarchy by using the built-in tree layout:<\/p>\n<pre>TreeLayout layout = new TreeLayout();\nlayout.Type = TreeLayoutType.Centered;\nlayout.LinkStyle = TreeLayoutLinkType.Cascading3;\nlayout.Direction = TreeLayoutDirections.TopToBottom;\nlayout.KeepRootPosition = false;\nlayout.LevelDistance = 40;\nlayout.Arrange(diagram);<\/pre>\n<p>The following image illustrates the result:<\/p>\n<p><a href=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/07\/Diagramming_Wpf_Templates.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1596\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/07\/Diagramming_Wpf_Templates-300x225.png\" alt=\"Diagramming_Wpf_Templates\" width=\"300\" height=\"225\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/07\/Diagramming_Wpf_Templates-300x225.png 300w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/07\/Diagramming_Wpf_Templates-768x576.png 768w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/07\/Diagramming_Wpf_Templates.png 1024w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/07\/Diagramming_Wpf_Templates-400x300.png 400w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>The source code of the project together with all necessary libraries can be downloaded from here:<\/p>\n<p><a href=\"https:\/\/mindfusion.dev\/_samples\/WpfDiagrammingTemplates.zip\">Download Organization Hierarchy Sample<\/a><\/p>\n<p>You are welcome to ask any questions about the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/wpfdiagram\/index.htm?T_MindFusion_Diagramming_Wpf_Diagram.htm\">Diagram<\/a> control at <a href=\"http:\/\/mindfusion.dev\/Forum\/YaBB.pl?board=wpfdg_disc\">MindFusion discussion board<\/a> or per e-mail at <a href=\"mailto:support@mindfusion.dev\">support@mindfusion.dev<\/a>.<\/p>\n<p>Click here <a href=\"http:\/\/mindfusion.dev\/wpf-diagram.html\">here<\/a> to visit the official page of the WPF diagram control.<\/p>\n<p>We hope you find this tutorial useful and thank you for your interest in MindFusion developer tools.<\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog you will learn how to create a presentation of a hierarchical organization using MindFusion WPF Diagram control. The hierarchy is presented as a diagram, where each node represents an employee in the organization and each link represents &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/mindfusion-diagramming-for-wpf-custom-templates\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[95,74],"tags":[417,35,414,419,415,58,416,418],"class_list":["post-1588","post","type-post","status-publish","format-standard","hentry","category-diagramming-2","category-sample-code","tag-custom-template","tag-diagramming","tag-hierarchy","tag-hierarchy-chart-in-wpf","tag-organization","tag-wpf","tag-wpf-diagram-control","tag-wpf-org-chart"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-pC","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1588","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=1588"}],"version-history":[{"count":13,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1588\/revisions"}],"predecessor-version":[{"id":2596,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1588\/revisions\/2596"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=1588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=1588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=1588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}