This example registers FactoryItemTools
for regular shapes and shapes created from SVG images/ShapeTemplates
. The FactoryItemTool allows you to add pre-configured or customized shapes and their descendants to stencils. The DiagramControl displays shapes specified by registered FactoryItemTools
in the Shapes Panel.
To register a FactoryItemTool for a regular shape, you must:
-
Create a stencil or use an existing stencil:
void RegisterStencil() { var stencil = new DiagramStencil("CustomStencil", "Custom Shapes"); RegularFactoryItemTool(stencil); FactoryItemToolForCustomShape(stencil); DiagramToolboxRegistrator.RegisterStencil(stencil); diagramControl1.OptionsBehavior.SelectedStencils = new StencilCollection() { "CustomStencil" }; }
-
Create a FactoryItemTool that specifies a diagram shape.
-
Pass your FactoryItemTool instance to the DiagramStencil.RegisterTool method:
public void RegularFactoryItemTool(DiagramStencil stencil) { var itemTool = new FactoryItemTool("CustomShape1", () => "Custom Shape 1", diagram => new DiagramShape() { Content = "Predefined text" }, new System.Windows.Size(200, 200), false ); stencil.RegisterTool(itemTool); }
You should create two stencils to register a FactoryItemTool for a custom shape:
-
Create an invisible stencil that contains your custom shape.
-
Create a visible stencil that contains the custom tool. This tool should access your custom shape by its
ID
from the invisible stencil:public void FactoryItemToolForCustomShape(DiagramStencil stencil) { DiagramControl.ItemTypeRegistrator.Register(typeof(DiagramShapeEx)); var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsFormsApp4.CustomShapes.xml"); var invisibleStencil = DiagramStencil.Create("InvisibleStencil", "Invisible Stencil", stream, shapeName => shapeName, false); DiagramToolboxRegistrator.RegisterStencil(invisibleStencil); var itemTool = new FactoryItemTool("CustomShape2", () => "Custom Shape 2", diagram => new DiagramShapeEx() { Shape = DiagramToolboxRegistrator.GetStencil("InvisibleStencil").GetShape("Shape1"), CustomProperty = "Some value" }, new System.Windows.Size(200, 200), false ); stencil.RegisterTool(itemTool); }
- Form1.cs (VB: Form1.vb)
- CustomShapes.xml