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 DiagramDesignerControl 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.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));
ResourceDictionary customShapesDictionary = new ResourceDictionary() { Source = new Uri("CustomShapes.xaml", UriKind.Relative) };
var invisibleStencil = DiagramStencil.Create("InvisibleStencil", "Invisible Stencil", customShapesDictionary, 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);
}
- WPF DiagramControl - Create Custom Shapes Based on Diagram Containers
- WPF DiagramControl - Create Custom Shapes with Connection Points
(you will be redirected to DevExpress.com to submit your response)