# In Code-Behind

This article shows how to define a `RadDiagram`, shapes, and connections in code-behind with Progress® Telerik® UI for WPF.

> note The examples use the Telerik Windows8 theme. See [setting an application-wide theme](https://www.telerik.com/products/wpf/documentation/styling-and-appearance/styling-apperance-setting-a-theme-overview) for the Xaml and NoXaml theme setup options.

## Adding Items In Code Behind

Before using the examples, configure a WPF project with the package or assembly references described in [RadDiagram Getting Started](https://www.telerik.com/products/wpf/documentation/controls/raddiagram/getting-started). Complete the [license activation](https://www.telerik.com/products/wpf/documentation/installation/licensing/installing-license-key) required for your Telerik release and configure the theme for your Xaml or NoXaml assemblies.

The following C# and VB.NET examples create a `RadDiagram` and add two shapes and a connection:

```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);
}
```

```VB.NET
Private Sub GenerateRadDiagram()
    Dim diagram As New RadDiagram()

    Dim shapeCloud As New RadDiagramShape() With
    {
        .Width = 180,
        .Height = 100, .Position = New Point() With {.X = 100, .Y = 100},
        .Content = "Cloud"
    }
    shapeCloud.Geometry = ShapeFactory.GetShapeGeometry(CommonShapeType.CloudShape)

    Dim dbShape As New RadDiagramShape() With
    {
      .Width = 80,
      .Height = 100,
      .Position = New Point() With {.X = 400, .Y = 100},
      .Content = "DB"
    }
    dbShape.Geometry = ShapeFactory.GetShapeGeometry(FlowChartShapeType.Database2Shape)

    Dim connection As New RadDiagramConnection() With
    {
       .Source = shapeCloud,
       .SourceConnectorPosition = ConnectorPosition.Right,
       .Target = dbShape,
       .TargetConnectorPosition = ConnectorPosition.Left,
       .SourceCapType = CapType.Arrow4,
       .TargetCapType = CapType.Arrow5Filled,
       .Content = New TextBlock() With
           {
             .Text = " Connection ",
             .Foreground = New SolidColorBrush() With
                {
                    .Color = Colors.Blue
                }
            }
    }
    diagram.Items.Add(shapeCloud)
    diagram.Items.Add(dbShape)
    diagram.Items.Add(connection)
    diagram.SelectAll()

    Me.LayoutRoot.Children.Add(diagram)
End Sub
```

The examples use the static `ShapeFactory` class from the `Telerik.Windows.Controls.Diagrams` assembly. The following C# and VB.NET examples list its static methods for creating an `EllipseShape`, `RectangleShape`, or `Geometry`:

```C#
public static RadDiagramShape CreateCircle(double radiusX, double radiusY, Point center)
{
    return new RadDiagramShape();
}
public static RadDiagramShape CreateRectangle(Rect rect)
{
    return new RadDiagramShape();
}
public static Geometry GetShapeGeometry(ArrowShapeType shapeType)
{
    return ShapeFactory.GetShapeGeometry(shapeType);
}
public static Geometry GetShapeGeometry(CommonShapeType shapeType)
{
    return ShapeFactory.GetShapeGeometry(shapeType);
}
public static Geometry GetShapeGeometry(FlowChartShapeType shapeType)
{
    return ShapeFactory.GetShapeGeometry(shapeType);
}
```

```VB.NET
Public Shared Function CreateCircle(ByVal radiusX As Double, ByVal radiusY As Double, ByVal center As Point) As RadDiagramShape
Public Shared Function CreateRectangle(ByVal rect_Renamed As Rect) As RadDiagramShape
Public Shared Function GetShapeGeometry(ByVal shapeType As ArrowShapeType) As Geometry
Public Shared Function GetShapeGeometry(ByVal shapeType As CommonShapeType) As Geometry
Public Shared Function GetShapeGeometry(ByVal shapeType As FlowChartShapeType) As Geometry
```

Here you see a snapshot of the defined `RadDiagram`:
![RadDiagram with a cloud labeled Cloud connected to a database shape labeled DB](https://www.telerik.com/products/wpf/documentation/assets/84217886e1066291084ee5beaa2378f5/raddiagrams-populating-with-data-in-code-behind.png)

## See Also

* [Structure](https://www.telerik.com/products/wpf/documentation/controls/raddiagram/structure)
* [Getting Started](https://www.telerik.com/products/wpf/documentation/controls/raddiagram/getting-started)
* [Declaratively Populating with Data](https://www.telerik.com/products/wpf/documentation/controls/raddiagram/populating-with-data/data-declaratively)
* [DataBinding](https://www.telerik.com/products/wpf/documentation/controls/raddiagram/populating-with-data/data-databinding)
* [Shapes](https://www.telerik.com/products/wpf/documentation/controls/raddiagram/diagram-items/shape/raddiagrams-features-shapes)
* [Connections](https://www.telerik.com/products/wpf/documentation/controls/raddiagram/diagram-items/raddiagrams-features-connections)
* [Removing Items](https://www.telerik.com/products/wpf/documentation/controls/raddiagram/items-manipulation/raddiagrams-features-delete)
