Back to Devexpress

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

windowsforms-116966-controls-and-libraries-diagrams-examples-how-to-change-the-set-of-shapes-available-in-the-shapes-panel.md

latest4.9 KB
Original Source

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

  • Oct 29, 2020
  • 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.

csharp
List<DiagramStencil> Stencils = DiagramToolboxRegistrator.Stencils.ToList<DiagramStencil>();
Stencils.ForEach(s => DiagramToolboxRegistrator.UnregisterStencil(s));
vb
Dim Stencils As List(Of DiagramStencil) = DiagramToolboxRegistrator.Stencils.ToList()
Stencils.ForEach(Function(s) DiagramToolboxRegistrator.UnregisterStencil(s))

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

csharp
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");
    }
vb
Public Class DiagramShapeEx
        Inherits DiagramShape

        <XtraSerializableProperty, Category("Info")>
        Public Property Status() As Status
    End Class

    Public Enum Status
        Active
        Inactive
    End Enum

    Public Sub RegisterShapes()
        DiagramControl.ItemTypeRegistrator.Register(GetType(DiagramShapeEx))
        Dim stencil = New DiagramStencil("customShapes", "Custom Shapes")
        stencil.RegisterTool(New FactoryItemTool("activeTaskShape", Function() "Active Task", Function(diagram) New DiagramShapeEx With {
            .Content = "Active Task",
            .Status = Status.Active
        }, New System.Windows.Size(150, 100), False))
        stencil.RegisterTool(New FactoryItemTool("inactiveTaskShape", Function() "Inactive Task", Function(diagram) New DiagramShapeEx With {
            .Content = "Inactive Task",
            .Status = Status.Inactive
        }, New System.Windows.Size(150, 100), False))
        DiagramToolboxRegistrator.RegisterStencil(stencil)
        diagramControl1.OptionsBehavior.SelectedStencils = StencilCollection.Parse("customShapes")
    End Sub

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.