Version

Configuring the Visualization of the Content of the Diagram Items (xamDiagram)

Topic Overview

Purpose

This topic explains how to set DataTemplates for the content of diagram items and for editing that content.

Required background

The following topics are prerequisites to understanding this topic:

Topic Purpose

This topic provides a conceptual overview of the xamDiagram control and its main features and capabilities.

This topic explains how to add the xamDiagram control to a WPF application.

Introduction

Diagram items templates summary

Diagram items have two templates that can be applied to their content – display template and edit template. The display template defines the presentation of the content, and the edit template is the template applied when editing the item’s content.

The following screenshot demonstrates the default content and edit templates applied to a diagram node.

Display Template Edit Template
xamDiagram Configuring the Diagram Items Content Visualization 1.png
xamDiagram Configuring the Diagram Items Content Visualization 2.png

Diagram items’ content visualization configuration summary

Manage the visual presentation of the content of the diagram items (nodes and connections) with a display template. Additionally, a set of Node-Level properties provide a convenient way to handle the most trivial aspects of content visualization, such as horizontal alignment and margins, and the element flow relative to the container element (fit in / overlap).

The display template is supplied pre-defined and applied to the diagram items by default. The default data template specifies a template for the content container. That template is the same for both nodes and connections: a TextBlock control with its Text property bound to the DiagramItem.Content value (with the only difference being a 2 px margin around the text block for nodes).

To control the positioning of the nodes’ content within the content container, nodes have a set of explicit properties, enabling you to configure the positioning without having to define it in the template. These properties are:

For diagram connections, the content is always positioned in the middle of the connection line.

Note
Note

Do not confuse the shape of the diagram node with the content container defined in the data template – these are two separate entities. Unlike the nodes in some popular diagramming software, the node shape itself cannot present content and the content container is not equivalent to the node shape (though in the template it can be configured as a replacement of the node shape in some advanced scenarios).

Note
Note

For diagram nodes, the DisplayTemplate is always applied regardless of the settings of the ShapeType and Geometry properties. Suggesting that you are expected to set the shape of the node using these properties. Define the presentation of the content in the DisplayTemplate and, if necessary, further fine-tune this presentation with the properties listed above. (For details on how to configure the node shape refer to Configuring the Shape of the Diagram Nodes.)

Diagram items’ content visualization configuration summary chart

The following table briefly explains the configurable aspects of the content of the diagram nodes and connections and maps them to the properties that configure them.

Mode Configurable aspect Details Properties

Display

Horizontal margins of content

The horizontal margins (the blank space between the left and right edges of the content and the side of the container) are configured individually for the left and the right margin with separate properties.

Alignment of content

The horizontal and vertical alignment are configured individually with separate properties.

  • HorizontalContentAlignment

  • VerticalContentAlignment

Content layout behavior

If the content size is greater than the node size, it can be either cropped to fit the container or displayed as is (based on the DisplayTemplate). In the latter case, the content will flow outside of the container overlapping it.

All aspects (template)

Configure the manner the content of a diagram node or connection displays with a DataTemplate. If the DisplayTemplate is not explicitly set, the default template mentioned above will be applied.

Edit

All aspects (template)

The way the content of a diagram node or connection is displayed in edit mode can be configured with a DataTemplate. If the EditTemplate is not explicitly set, the default template mentioned above will be applied.

Configuring a Custom Display Template

Overview

Applying a custom template for a diagram node or a diagram connection is done by setting the DisplayTemplate property to a DataTemplate in which the value of the Content property is internally set as the DataContext of the data template.

Property settings

The following table maps the desired configuration to the property settings that manage it.

In order to: Use this property: And set it to:

Set a custom display template

The desired DataTemplate.

Example

The screenshot below demonstrates how a node and a connection in the xamDiagram would look as a result of the following settings:

Property Value

“Button Content Template”

In XAML:

<DataTemplate>
<Button Content="{Binding}" />
</DataTemplate>

Orange

In XAML:

<DataTemplate>
<Ellipse
Fill="{Binding}"
Stroke="{Binding Stroke, RelativeSource={RelativeSource AncestorType=ig:DiagramConnection}}"
Width="20" Height="20"/>
</DataTemplate>
xamDiagram Configuring the Diagram Items Content Visualization 3.png

Following is the code that implements this example.

In XAML:

<ig:XamDiagram>
<ig:DiagramNode Content="Button Content Template" Width="160">
<ig:DiagramNode.DisplayTemplate>
<DataTemplate>
<Button Content="{Binding}" />
</DataTemplate>
</ig:DiagramNode.DisplayTemplate>
</ig:DiagramNode>
<ig:DiagramConnection Content="Orange" StartPosition="0,150" EndPosition="120,150">
<ig:DiagramConnection.DisplayTemplate>
<DataTemplate>
<!-- Binding the ellipse's fill to the connection's
Content and the Stroke to the connection's Stroke -$$->$$
<Ellipse
Fill="{Binding}"
Stroke="{Binding Stroke, RelativeSource={RelativeSource AncestorType=ig:DiagramConnection}}"
Width="20" Height="20"/>
</DataTemplate>
</ig:DiagramConnection.DisplayTemplate>
</ig:DiagramConnection>
</ig:XamDiagram>

Configuring a Custom Edit Template

Overview

Applying a custom edit template for a diagram node or a diagram connection is done by setting the EditTemplate property to a DataTemplate. The value of the Content property is internally as the DataContext of the data template.

Property settings

The following table maps the desired configuration to the property settings that manage it.

In order to: Use this property: And set it to:

Set a custom content edit template

The desired DataTemplate.

Example

The screenshot below demonstrates how a node and a connection in the xamDiagram would look as a result of the following settings and when in edit mode:

Property Value

“New Content?”

In XAML:

<DataTemplate>
<StackPanel>
<TextBlock Text="Enter Display Text:" Foreground="WhiteSmoke" />
<TextBox Text="{Binding Content, RelativeSource={RelativeSource AncestorType=ig:DiagramNode}}"/>
</StackPanel>
</DataTemplate>

Orange

In XAML:

<DataTemplate>
<ComboBox SelectedItem="{Binding .}">
<sys:String>Orange</sys:String>
<sys:String>Green</sys:String>
<sys:String>Blue</sys:String>
<sys:String>Red</sys:String>
<ComboBox.ItemTemplate>
<DataTemplate>
<Ellipse Fill="{Binding Content, RelativeSource={RelativeSource AncestorType=ig:DiagramConnection}}" Width="20" Height="20"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
xamDiagram Configuring the Diagram Items Content Visualization 4.png

Following is the code that implements this example.

In XAML:

<ig:XamDiagram xmlns:sys="clr-namespace:System;assembly=mscorlib">
<ig:DiagramNode Content="Button Content Template" Width="160">
<ig:DiagramNode.EditTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="Enter Display Text:" Foreground="WhiteSmoke" />
<TextBox Text="{Binding Content, RelativeSource={RelativeSource AncestorType=ig:DiagramNode}}"/>
</StackPanel>
</DataTemplate>
</ig:DiagramNode.EditTemplate>
<ig:DiagramNode.DisplayTemplate>
<DataTemplate>
<Button Content="{Binding}" />
</DataTemplate>
</ig:DiagramNode.DisplayTemplate>
</ig:DiagramNode>
<ig:DiagramConnection Content="Orange" StartPosition="0,150" EndPosition="120,150">
<ig:DiagramConnection.EditTemplate>
<DataTemplate>
<!-- Binding the ComboBox's SelectedItem to the connection's Content. If a complex object
is set as the connection's Content, a simple binding with a Path set to the property path
of the property that has to be edited is enough. -$$->$$
<ComboBox SelectedItem="{Binding Content, RelativeSource={RelativeSource AncestorType=ig:DiagramConnection}}">
<sys:String>Orange</sys:String>
<sys:String>Green</sys:String>
<sys:String>Blue</sys:String>
<sys:String>Red</sys:String>
<ComboBox.ItemTemplate>
<DataTemplate>
<Ellipse Fill="{Binding}" Width="20" Height="20"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</ig:DiagramConnection.EditTemplate>
<ig:DiagramConnection.DisplayTemplate>
<DataTemplate>
<!-- Binding the ellipse's fill to the connection's
Content and the Stroke to the connection's Stroke -$$->$$
<Ellipse
Fill="{Binding}"
Stroke="{Binding Stroke, RelativeSource={RelativeSource AncestorType=ig:DiagramConnection}}"
Width="20" Height="20"/>
</DataTemplate>
</ig:DiagramConnection.DisplayTemplate>
</ig:DiagramConnection>
</ig:XamDiagram>

Related Topics

The following topic provides additional information related to this topic.

Topic Purpose

This topic explains how to configure the shape of the diagram nodes of the xamDiagram control by either selecting a pre-defined shape or applying a custom shape.