How to: Change the Set of Shapes Available In the Shapes Panel

  • 2 minutes to read

Shapes in the Shapes Panel are grouped into sets called stencils. Stencils are represented by the DiagramStencil class. The DiagramStencil.Shapes and DiagramStencil.ContainerShapes properties return the stencil's shapes and containers, and the DiagramStencil.IsVisible property indicates whether the stencil is available in the Shapes Panel.

To add a shape to a stencil, use the stencil's DiagramStencil.RegisterShape method.

The DiagramToolboxRegistrator class provides methods for modifying stencils. The DiagramToolboxRegistrator.Stencils property returns a list of stencils. To add or remove stencils from the list, use the DiagramToolboxRegistrator.RegisterStencil and DiagramToolboxRegistrator.UnregisterStencil methods. The code snippet below illustrates how to remove all the default stencils from the Shapes Panel.


List<DiagramStencil> Stencils = DiagramToolboxRegistrator.Stencils.ToList<DiagramStencil>();
Stencils.ForEach(s => DiagramToolboxRegistrator.UnregisterStencil(s));

To add custom diagram items to a stencil, use the DiagramStencil.RegisterTool method.


public class DiagramShapeEx : DiagramShape {

        [XtraSerializableProperty, Category("Info")]
        public Status Status { get; set; }
    }

    public enum Status { Active, Inactive }

    public void RegisterShapes() {
        DiagramControl.ItemTypeRegistrator.Register(typeof(DiagramShapeEx));
        var stencil = new DiagramStencil("customShapes", "Custom Shapes");
        stencil.RegisterTool(new FactoryItemTool("activeTaskShape", () => "Active Task", diagram => new DiagramShapeEx { Content = "Active Task", Status = Status.Active }, new System.Windows.Size(150, 100), false));
        stencil.RegisterTool(new FactoryItemTool("inactiveTaskShape", () => "Inactive Task", diagram => new DiagramShapeEx { Content = "Inactive Task", Status = Status.Inactive }, new System.Windows.Size(150, 100), false));
        DiagramToolboxRegistrator.RegisterStencil(stencil);
        diagramControl1.OptionsBehavior.SelectedStencils = StencilCollection.Parse("customShapes");
    }
NOTE

The diagram control loads its stencils from the DiagramToolboxRegistrator.Stencils collection. You can use the DiagramOptionsBehavior.Stencils property if your application contains multiple diagram controls, and you want to specify individual sets of stencils for certain controls.