New to Telerik UI for WPF? Start a free 30-day trial
In Code-Behind
Updated on Sep 24, 2025
This article will demonstrate how to define RadDiagram, Shapes and Connections in code behind
Please note that the examples in this tutorial are showcasing Telerik Windows8 theme. In the Setting a Theme article you can find more information on how to set an application-wide theme.
Adding Items In Code Behind
Below you can find a code snippet which creates a RadDiagram and adds two Shapes and a Connection in it:
C#
private void GenerateRadDiagram()
{
RadDiagram diagram = new RadDiagram();
RadDiagramShape shapeCloud = new RadDiagramShape()
{
Width = 180,
Height = 100,
Position = new Point() { X = 100, Y = 100},
Content = "Cloud"
};
shapeCloud.Geometry = ShapeFactory.GetShapeGeometry(CommonShapeType.CloudShape);
RadDiagramShape dbShape = new RadDiagramShape()
{
Width = 80,
Height = 100,
Position = new Point() { X = 400, Y = 100 },
Content = "DB"
};
dbShape.Geometry = ShapeFactory.GetShapeGeometry(FlowChartShapeType.Database2Shape);
RadDiagramConnection connection = new RadDiagramConnection()
{
Source = shapeCloud,
SourceConnectorPosition = ConnectorPosition.Right,
Target = dbShape,
TargetConnectorPosition = ConnectorPosition.Left,
SourceCapType = CapType.Arrow4,
TargetCapType = CapType.Arrow5Filled,
Content = new TextBlock() { Text = " Connection ", Foreground = new SolidColorBrush() { Color = Colors.Blue} }
};
diagram.Items.Add(shapeCloud);
diagram.Items.Add(dbShape);
diagram.Items.Add(connection);
diagram.SelectAll();
this.LayoutRoot.Children.Add(diagram);
}In the code above, we use the static class ShapeFactory from the Telerik.Windows.Controls.Diagrams assembly. Below you can find the list of its static methods for creating an EllipseShape, RectangleShape or Geometry:
C#
public static RadDiagramShape CreateCircle(double radiusX, double radiusY, Point center)
public static RadDiagramShape CreateRectangle(Rect rect)
public static Geometry GetShapeGeometry(ArrowShapeType shapeType)
public static Geometry GetShapeGeometry(CommonShapeType shapeType)
public static Geometry GetShapeGeometry(FlowChartShapeType shapeType)Here you see a snapshot of the defined RadDiagram:
