This tutorial builds upon the OrgChartNode type from Tutorial 3. It shows how to fully integrate the custom type with other DiagramLite features, such as serialization, undo support, creating nodes by drag-and-drop or drawing with the mouse on the canvas.
- Add a NodeListView to the page and add a few OrgChartNode objects as its children:
Xaml
Copy Code
|
---|
<diag:NodeListView Width="150"> <t:OrgChartNode Bounds="0,0,140,70" Title="CEO" FullName="Type here" Text="Chief executive officer" ImageWebLocation="016.png" /> <t:OrgChartNode Bounds="0,0,140,70" Title="CIO" FullName="Type here" Text="Chief information officer" ImageWebLocation="ac0026-64.png" /> </diag:NodeListView> |
- Add a copy constructor to the OrgChartNode class. The control throws an exception if you try to create via drag-and-drop an instance of a class without copy constructor:
C#
Copy Code
|
---|
public OrgChartNode(OrgChartNode prototype) : base(prototype) { DefaultStyleKey = typeof(OrgChartNode); Title = prototype.Title; FullName = prototype.FullName; }
|
- In order to enable drawing OrgChartNodes with the mouse, inherit the LinkNodesBehavior and override its CreateNode method. You could also inherit BehaviorBase or any of its derived classes, and override either CreateNode or StartDraw:
C#
Copy Code
|
---|
public class OrgChartBehavior : LinkNodesBehavior { public OrgChartBehavior(Diagram diagram) : base(diagram) { }
protected override DiagramNode CreateNode() { return new OrgChartNode(); } } |
- Assign an instance of the behavior class to the CustomBehavior property of the Diagram class:
C#
Copy Code
|
---|
private void UserControl_Loaded(object sender, RoutedEventArgs e) { diagram.CustomBehavior = new OrgChartBehavior(diagram); } |
- To implement serialization of any new properties added by a custom item class, override the SaveToXml and LoadFromXml methods of the base DiagramItem class:
C#
Copy Code
|
---|
protected override void SaveToXml(System.Xml.Linq.XElement xmlElement, XmlPersistContext context) { base.SaveToXml(xmlElement, context); context.WriteString(Title, "Title", xmlElement); context.WriteString(FullName, "FullName", xmlElement); }
protected override void LoadFromXml(System.Xml.Linq.XElement xmlElement, XmlPersistContext context) { base.LoadFromXml(xmlElement, context); Title = context.ReadString("Title", xmlElement); FullName = context.ReadString("FullName", xmlElement); }
|
- Call the static RegisterItemClass method to assign an XML element name to the custom item type:
C#
Copy Code
|
---|
private void UserControl_Loaded(object sender, RoutedEventArgs e) { Diagram.RegisterItemClass(typeof(OrgChartNode), "my:OrgChartNode", 1);
diagram.CustomBehavior = new OrgChartBehavior(diagram); } |
- In order to enable undo / redo of changes done on OrgChartNode, create a DiagramItemState -derived class that contains member corresponding to the additional properties defined by OrgChartNode:
C#
Copy Code
|
---|
public class OrgChartNodeState : ShapeNodeState { internal string Title; internal string FullName; } |
- Override the CreateState, SaveState and RestoreState methods:
C#
Copy Code
|
---|
protected override DiagramItemState CreateState() { return new OrgChartNodeState(); }
protected override DiagramItemState SaveState() { var state = (OrgChartNodeState)base.SaveState(); state.Title = Title; state.FullName = FullName; return state; }
protected override void RestoreState(DiagramItemState itemState) { base.RestoreState(itemState);
var state = (OrgChartNodeState)itemState; Title = state.Title; FullName = state.FullName; }
|
The full source code for this tutorial with several additional UI controls that invoke serialization and undo methods is available in the Samples\CSharp\Tutorial4 subfolder under the DiagramLite installation folder.