Example T320892
Visible to All Users

WPF DiagramControl - Create Custom Shapes with Connection Points

This example demonstrates how to define custom shapes for the Diagram Designer Control in the XAML markup.

image

The ShapeTemplate class describes a diagram shape and contains the following segments:

  • Start - Specifies the start point of the geometry and includes customization properties. A shape can consist of multiple geometries.
  • Line - Defines a line with start and end points.
  • Arc - Defines an arc with size and direction.
  • ShapePoint - Defines a connection point. To specify connection points, use the ShapeTemplate.ConnectionPoints property.
  • Parameter - Defines a parameter that allows users to transform the shape. To specify parameters, use the ShapeTemplate.Parameters property.

Refer to the following help topic for more information about supported segments: Creating Shapes and Containers Using Shape Templates.

To register custom shapes, create a stencil with the DiagramStencil.Create method and pass it to the DiagramToolboxRegistrator.RegisterStencil method.

To create a shape in code, get an instance of your custom stencil, call the DiagramStencil.GetShape method to access the ShapeDescription object, and assign it to the DiagramShape.Shape property:

C#
new DiagramShape() { Shape = customStencil.GetShape("Shape1")}

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)

Example Code

DXDiagram.CreateCustomShapes/CustomShapes.xaml
XAML
<p:ResourceDictionary xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.devexpress.com/winfx/2008/xaml/diagram" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ShapeTemplate x:Key="{ShapeKey Shape1}" DefaultSize="50, 100" > <Start X="0" Y="0"/> <Line X="1" Y="0" /> <Line X="0.8" Y="0.8" /> <Line X="0" Y="1" /> <ShapeTemplate.ConnectionPoints> <ShapePoint X="0" Y="0" /> <ShapePoint X="0.5" Y="0" /> <ShapePoint X="1" Y="0" /> <ShapePoint X="0.8" Y="0.8" /> <ShapePoint X="0" Y="1" /> <ShapePoint X="0" Y="0.5" /> </ShapeTemplate.ConnectionPoints> </ShapeTemplate> <!--W - shape width--> <!--H - shape height--> <ShapeTemplate x:Key="{ShapeKey Shape2}" DefaultSize="60, 120"> <Start X="0" Y="0" FillColor="Brown"/> <Arc X="1" Y="0" Size="CreateSize(W/2, H/10)" Direction="Counterclockwise" /> <Arc X="1" Y="1" Size="CreateSize(W/10, H/2)" Direction="Counterclockwise" /> <Arc X="0" Y="1" Size="CreateSize(W/2, H/10)" Direction="Counterclockwise" /> <Arc X="0" Y="0" Size="CreateSize(W/10, H/2)" Direction="Counterclockwise" /> <ShapeTemplate.ConnectionPoints> <ShapePoint X="0" Y="0" /> <ShapePoint X="1" Y="0" /> <ShapePoint X="0" Y="1" /> </ShapeTemplate.ConnectionPoints> </ShapeTemplate> <!--P0 - parameter 0--> <!--P in Point definition - parameter value --> <!--P in Value definition - parameter point--> <ShapeTemplate x:Key="{ShapeKey Shape3}" DefaultSize="60, 120"> <Start X="0" Y="0" FillColor="Brown"/> <Arc X="1" Y="0" Size="CreateSize(W/2, P0 * H)" Direction="Counterclockwise" /> <Line X="1" Y="1"/> <Line X="0" Y="1"/> <Line X="0" Y="0"/> <ShapeTemplate.ConnectionPoints> <ShapePoint X="0" Y="0" /> <ShapePoint X="1" Y="0" /> <ShapePoint X="0" Y="1" /> </ShapeTemplate.ConnectionPoints> <ShapeTemplate.Parameters> <Parameter DefaultValue="0.1" Point="CreatePoint(W / 2, P * H)" Value="P.Y / H" Min="0" Max="1" /> </ShapeTemplate.Parameters> </ShapeTemplate> <!--this shape contains two rows--> <ShapeTemplate x:Key="{ShapeKey Shape4}" DefaultSize="60, 120" Rows="H*P0;H*(1-P0)" IsQuick="True"> <Start X="0" Y="0" FillColor="Green"/> <Line X="1" Y="0"/> <Line X="1" Y="1"/> <Line X="0" Y="1"/> <Start X="0" Y="1" FillColor="Red"/> <Line X="1" Y="1"/> <Line X="1" Y="2"/> <Line X="0" Y="2"/> <ShapeTemplate.ConnectionPoints> <ShapePoint X="0.5" Y="0" /> <ShapePoint X="1" Y="1" /> <ShapePoint X="0.5" Y="2" /> <ShapePoint X="0" Y="1" /> </ShapeTemplate.ConnectionPoints> <ShapeTemplate.Parameters> <Parameter DefaultValue="0.5" Point="CreatePoint(W, P * H)" Value="P.Y / H" Min="0" Max="1" /> </ShapeTemplate.Parameters> </ShapeTemplate> </p:ResourceDictionary>
DXDiagram.CreateCustomShapes/MainWindow.xaml
XAML
<Window x:Class="DXDiagram.CreateCustomShapes.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:dxdiag="http://schemas.devexpress.com/winfx/2008/xaml/diagram" xmlns:local="clr-namespace:DXDiagram.CreateCustomShapes" mc:Ignorable="d" Title="MainWindow" Height="600" Width="800"> <Grid> <dxdiag:DiagramDesignerControl Name="diagramControl"/> </Grid> </Window>
DXDiagram.CreateCustomShapes/MainWindow.xaml.cs(vb)
C#
using System; using System.Windows; using DevExpress.Diagram.Core; using DevExpress.Xpf.Core; namespace DXDiagram.CreateCustomShapes { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); InitCustomDiagramShapes(); } void InitCustomDiagramShapes() { ResourceDictionary customShapesDictionary = new ResourceDictionary() { Source = new Uri("CustomShapes.xaml", UriKind.Relative) }; var stencil = DiagramStencil.Create("MyShapes", "Custom Shapes", customShapesDictionary, shapeName => shapeName); DiagramToolboxRegistrator.RegisterStencil(stencil); diagramControl.SelectedStencils.Add("MyShapes"); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.