Generate Organization Charts

  • 6 minutes to read

This topic describes how to bind the DiagramControl to a hierarchical data source.

Tip

Refer to the Generate Diagrams from a Data Source topic for information on how to bind the DiagramControl to a data source with an arbitrary structure.

Add the Behavior

The DiagramOrgChartBehavior is used to bind the DiagramControl to organization chart data. To add the behavior, open the DiagramControl’s Quick Actions and select DiagramOrgChartBehavior.

Diagram data behaviors smart tag

The following XAML is generated.

<dxdiag:DiagramControl SelectedStencils="BasicShapes, BasicFlowchartShapes">
    <dxmvvm:Interaction.Behaviors>
        <dxdiag:DiagramOrgChartBehavior />
    </dxmvvm:Interaction.Behaviors>
</dxdiag:DiagramControl>

To configure the data source, switch to the Quick Actions’s Behaviors tab.

OrgChart Hierarchical

Tip

The Item Template Designer allows you to create templates for diagram items and connectors at design time.

Configure the binding according to the data source type:

Bind to a Self-Referencing Data Source

Each item in a self-referencing data source contains a unique item ID and the ID of its parent.

The following table lists the properties that map the DiagramControl to self-referencing data.

Member

Description

DiagramOrgChartBehavior.ItemsSource

(via DiagramDataBindingBehaviorBase.ItemsSource)

Specifies the path to a collection of diagram items.

DiagramDataBindingBehaviorBase.KeyMember

Specifies the name of the data field that identifies a diagram item.

DiagramOrgChartBehavior.ParentMember

Specifies the name of the data field that identifies the parent of a diagram item.

The following example demonstrates the DiagramControl bound to self-referencing data.

OrgChartBehaviorExample

<Window ...
        xmlns:dxdiag="http://schemas.devexpress.com/winfx/2008/xaml/diagram" 
        xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" >
    <Window.DataContext>
        <local:OrgChartViewModel/>
    </Window.DataContext>
    <Grid>
        <dxdiag:DiagramControl SelectedStencils="BasicShapes, BasicFlowchartShapes">
            <dxmvvm:Interaction.Behaviors>
                <dxdiag:DiagramOrgChartBehavior ItemsSource="{Binding Data}" KeyMember="Id" ParentMember="ParentId">
                    <dxdiag:DiagramOrgChartBehavior.TemplateDiagram>
                        <dxdiag:DiagramControl SelectedStencils="TemplateDesigner">
                            <dxdiag:DiagramShape Height="50"  Width="100">
                                <dxdiag:DiagramShape.Bindings>
                                    <dxdiag:DiagramBinding Expression="Name" PropertyName="Content" />
                                </dxdiag:DiagramShape.Bindings>
                            </dxdiag:DiagramShape>
                            <dxdiag:DiagramConnector Type="RightAngle"/>
                        </dxdiag:DiagramControl>
                    </dxdiag:DiagramOrgChartBehavior.TemplateDiagram>
                </dxdiag:DiagramOrgChartBehavior>
            </dxmvvm:Interaction.Behaviors>
        </dxdiag:DiagramControl>
    </Grid>
</Window>

Bind to a Hierarchical Data Source

A hierarchical data source usually contains a collection of root items. Each item stores a collection of its subordinates.

The following table lists the properties used to map the DiagramControl to hierarchical data.

Member

Description

DiagramOrgChartBehavior.ItemsSource

(via DiagramDataBindingBehaviorBase.ItemsSource)

Specifies the path to a collection of root items.

DiagramOrgChartBehavior.ChildrenPath

Specifies the path to the property that contains subordinates of a diagram item.

DiagramOrgChartBehavior.ChildrenSelector

Allows choosing an item’s subordinates based on custom logic.

The following example demonstrates the DiagramControl bound to hierarchical data.

OrgChartBehaviorExample2

<Window ...
        xmlns:dxdiag="http://schemas.devexpress.com/winfx/2008/xaml/diagram" 
        xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" >
    <Window.DataContext>
        <local:OrgChartViewModel/>
    </Window.DataContext>
    <Grid>
        <dxdiag:DiagramControl SelectedStencils="BasicShapes, BasicFlowchartShapes">
            <dxmvvm:Interaction.Behaviors>
                <dxdiag:DiagramOrgChartBehavior ItemsSource="{Binding Data}" ChildrenPath="Children">
                    <dxdiag:DiagramOrgChartBehavior.TemplateDiagram>
                        <dxdiag:DiagramControl SelectedStencils="TemplateDesigner">
                            <dxdiag:DiagramShape Height="50"  Width="100">
                                <dxdiag:DiagramShape.Bindings>
                                    <dxdiag:DiagramBinding Expression="Name" PropertyName="Content" />
                                </dxdiag:DiagramShape.Bindings>
                            </dxdiag:DiagramShape>
                            <dxdiag:DiagramConnector Type="RightAngle"/>
                        </dxdiag:DiagramControl>
                    </dxdiag:DiagramOrgChartBehavior.TemplateDiagram>
                </dxdiag:DiagramOrgChartBehavior>
            </dxmvvm:Interaction.Behaviors>
        </dxdiag:DiagramControl>
    </Grid></Window>

Expand and Collapse Subordinates

When you bind to a large data source, the generated diagram may occupy a lot of space on the canvas. To make the resulting diagram easier to navigate, the DiagramOrgChartBehavior introduces the expand/collapse functionality.

The following properties allow you to configure the expand/collapse behavior.

Property Description
DiagramOrgChartBehavior.ExpandSubordinatesButtonMode Controls the availability of the expand/collapse button.
DiagramOrgChartBehavior.GenerationDepth Specifies the number of hierarchy levels retrieved from the data source when the diagram is generated.
DiagramOrgChartBehavior.ExpansionDepth Specifies the number of hierarchy levels that expand when the diagram is generated.

The following example demonstrates an organization chart that displays three hierarchy levels. Additional shapes are generated and displayed when an end-user clicks the expand button.

ExpandCollapse Subordinates Animation

<dxdiag:DiagramControl SelectedStencils="BasicShapes, BasicFlowchartShapes">
    <dxmvvm:Interaction.Behaviors>
        <dxdiag:DiagramOrgChartBehavior ItemsSource="{Binding Data}" KeyMember="Id" ParentMember="ParentId" 
                                        GenerationDepth="2" ExpansionDepth="2" ExpandSubordinatesButtonMode="AlwaysVisible">
            <dxdiag:DiagramOrgChartBehavior.TemplateDiagram>
                <dxdiag:DiagramControl SelectedStencils="TemplateDesigner">
                    <dxdiag:DiagramShape Height="50"  Width="100">
                        <dxdiag:DiagramShape.Bindings>
                            <dxdiag:DiagramBinding Expression="Name" PropertyName="Content" />
                        </dxdiag:DiagramShape.Bindings>
                    </dxdiag:DiagramShape>
                    <dxdiag:DiagramConnector Type="RightAngle"/>
                </dxdiag:DiagramControl>
            </dxdiag:DiagramOrgChartBehavior.TemplateDiagram>
        </dxdiag:DiagramOrgChartBehavior>
    </dxmvvm:Interaction.Behaviors>
</dxdiag:DiagramControl>

Example

View Example

Video Tutorial

This video demonstrates how to use the Organization Chart functionality to generate relationship diagrams from the data source.