Skip to content

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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

About

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

Topics

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •