This tutorial builds upon the OrgChartNode type from Tutorial 3. It shows how to fully integrate the custom type with other WpfDiagram features, such as serialization, undo support, creating nodes by drag-and-drop or drawing with the mouse on the canvas.
1. Add a NodeListView to the page and add a few OrgChartNode objects as its children:
Xaml Copy Code |
---|
<Grid.Resources> |
2. 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 |
---|
// Required for creating nodes by dragging them from the NodeListView |
3. 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 protected override DiagramNode CreateNode() |
4. Assign an instance of the behavior class to the CustomBehavior property of the DiagramView class:
C# Copy Code |
---|
private void Window_Loaded(object sender, RoutedEventArgs e) |
5. 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 |
---|
// serialization support for custom properties protected override void SaveToXml(XmlElement xmlElement, XmlPersistContext context) protected override void LoadFromXml(XmlElement xmlElement, XmlPersistContext context) |
6. Call the static RegisterItemClass method to assign an XML element name to the custom item type:
C# Copy Code |
---|
private void Window_Loaded(object sender, RoutedEventArgs e) // enable drawing OrgChartNodes with the mouse |
7. In order to enable undo / redo of changes done on OrgChartNode, create a DiagramItemProperties -derived class that contains member corresponding to the additional properties defined by OrgChartNode:
C# Copy Code |
---|
public class OrgChartNodeProperties : DiagramNodeProperties |
8. Override the CreateProperties, SaveProperties and RestoreProperties methods:
C# Copy Code |
---|
// undo and redo support |
9. 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 WpfDiagram installation folder.