Skip to content

DevExpress-Examples/winforms-diagram-register-factoryitemtools-for-shapes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WinForms DiagramControl - Register FactoryItemTools for Regular and Custom Shapes

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.

Implementation Details

To register a FactoryItemTool for a regular shape, you must:

  1. 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" };
    }
  2. Create a FactoryItemTool that specifies a diagram shape.

  3. 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:

  1. Create an invisible stencil that contains your custom shape.

  2. 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);
    }

Files to Review

Documentation

More Examples

About

Register FactoryItemTools for regular shapes and shapes created from SVG images or ShapeTemplates.

Topics

Resources

License

Stars

Watchers

Forks