Data Binding for Graph Structures

yFiles WPF provides support for the WPF data binding mechanism, which enables a simple and consistent way to create and update graph structures from data sources.

Data Binding Support Overview

The yFiles WPF data binding support provides a convenient way of creating a graph structure from a data source. It bases on the WPF data binding mechanism and is tailored to specific use-cases where a tree structure or a general graph structure can be easily derived from a data model that meets certain conditions.

Along with the creation of the graph structure, the yFiles WPF data binding support also enables specifying the visual representation of nodes and edges in the graph, and also the hierarchical organization in a grouped graph (class GraphSource only). There is no layout algorithm applied once a graph is built, however, this can be easily done in a subsequent step.

Note

Even if a data model does not fit the specific use-cases that the data binding support classes are tailored to, the support might still be of use to accomplish an initial import of at least part of the data model (for example, the nodes).

Ultimately, though, please keep in mind that graphs can still be created the "normal way," i.e., by building and populating a graph structure using the functionality provided by the default IGraph implementation as presented in the section called “Graph Structure”.

Information related to the WPF data binding mechanism can be found in the .NET framework's documentation. For example:

Table 2.31, “Data binding classes” lists the classes that enable data binding for graph structures.

Table 2.31. Data binding classes

Type Name Description
TreeSource Enables creation of a tree graph structure from a data model where edges are defined implicitly. See the description of this class below.
GraphSource Provides data binding support to create a general graph structure from a data model. See the description of this class below.

Class TreeSource

The TreeSource class enables a data binding to create tree structures from a data model. The data model needs to provide the business objects for an initial set of nodes at least and a way of finding the business objects for further (child) nodes. Effectively, this means that the edges of the tree structures are only defined implicitly.

TreeSource supports creation of a set of tree structures that will live in the same graph, i.e., actually a "forest" can be created.

The data binding to create the tree structure(s) is specified using the following TreeSource properties:

  • NodesSource denotes the collection of business objects from the data model for which nodes will be created. At least an initial set of objects for which root nodes will be created needs to be specified. Specifying the business objects that correspond to the entire set of nodes in the resulting graph is supported as well.
  • ChildBinding denotes the binding to get from one business object, which already is in the collection denoted by NodesSource, to further business objects. Any further object that is not yet in the collection will be added to it.

    Subsequently, a corresponding node will be created for each new object in the collection and edges will be created that connect the node corresponding to the initial business object to each of the nodes corresponding to the further business object.

An additional binding that allows to specify the business objects for edge labels is provided through the EdgeLabelBinding property.

Building the Graph Structure

TreeSource will build the tree structure(s) recursively using the collection returned by the NodesSource property and the binding denoted by the ChildBinding property as follows: The initial set of business objects that starts the recursion is taken from the collection returned by the NodesSource property. Subsequently, the recursion uses the results from the ChildBinding. Accordingly, due to the recursive nature of the process, it is crucial that ChildBinding yields appropriate business objects that the ChildBinding can be applied to.

During the recursive process, the data context for ChildBinding is always a business object from the data model that corresponds to a node in the graph. The business object that is the data context for EdgeLabelBinding corresponds to a child node in the graph, i.e., to the target node of the respective edge.

Similar to the OrgChartWindow demo application, the XAML snippet in Example 2.51, “XAML definition of a TreeSource data binding” shows the definition of a TreeSource data binding to create an organization chart.

Example 2.51. XAML definition of a TreeSource data binding

<!-- Creating an organization chart graph from business data in an XML file. -->
<y:TreeSource ... 
    NodesSource="{Binding Source={StaticResource Staff}, XPath=employee}" 
    ChildBinding="{Binding XPath=employee}">
  ...
</y:TreeSource>

Class TreeSource offers the DynamicUpdates property to set the dynamic nature of the data binding. If the collection view of the graph's nodes can be queried for changes, enabling the property results in the graph structure being rebuilt whenever the ChildBinding or collection returned by the NodesSource property changes. The GraphRebuilt event will be triggered upon completion of the graph structure's rebuilding.

The visual representation of the nodes in the tree structures can be specified using a control style that is set by means of the NodeControlStyle property. Note that the target type of the control style needs to be a NodeControl. The visual style for the edges can be specified through the EdgeStyle property.

Class GraphSource

The GraphSource class enables a data binding to create a graph structure from a data model. The data model needs to provide the business objects for both the nodes and the edges. Also, for each edge both its end nodes need to be in the set of nodes.

Additionally, GraphSource also enables a data binding to create a hierarchical organization in a grouped graph. The data model needs to provide the business objects for the group nodes and ways of finding their contained (group) nodes.

The data binding to create the graph structure is specified using the following GraphSource properties:

  • NodesSource denotes the business objects from the data model for which nodes will be created, EdgesSource denotes the business objects from the data model for edges will be created subject to the results from the following bindings
  • SourceNodeBinding and TargetNodeBinding denote the binding to get from the business object for an edge to the business objects for its end nodes

Furthermore, the data binding to create the hierarchical organization can be specified using the following GraphSource properties:

  • GroupsSource denotes the business objects from the data model for which group nodes will be created
  • GroupBinding and ParentGroupBinding denote bindings to get from the business object for a node, respectively group node, to the business object for its containing group node

An additional binding that allows to specify the business objecte for edge labels is provided through the EdgeLabelBinding property.

Building the Graph Structure

GraphSource will build the graph structure using the collections returned by the NodesSource and EdgesSource properties and the bindings denoted by the SourceNodeBinding and TargetNodeBinding. The NodesSource property is queried first, then is the EdgesSource property. Along with the latter, the SourceNodeBinding and TargetNodeBinding bindings are evaluated. It is crucial that each of the bindings evaluates to a business object that is contained in the collection returned by the NodesSource property, otherwise no edge will be created for a given business object from the collection returned by the EdgesSource property.

The data context for the SourceNodeBinding and TargetNodeBinding is always a business object from the data model that corresponds to an edge in the graph. Similarly, the business object that is the data context for EdgeLabelBinding also corresponds to an edge in the graph.

The hierarchical organization of a grouped graph will be created using the collection returned by the GroupsSource property and the bindings denoted by the GroupBinding and ParentGroupBinding properties. If the business object that a binding evaluates to, is contained in the collection returned by the GroupsSource property, the corresponding group node is set the containing group node of the (group) node which corresponds to the business object that is used as the binding's data context.

The data context for GroupBinding is a business object from the data model that corresponds to a node in the graph, i.e., it is taken from the collection returned by the NodesSource property. The business object that is the data context for ParentGroupBinding corresponds to a group node, i.e., it is taken from the collection returned by the GroupsSource property.

Class GraphSource offers the DynamicUpdates property to set the dynamic nature of the data binding. If the collection view of the graph's nodes can be queried for changes, enabling the property results in the graph structure being rebuilt whenever the collection returned by the NodesSource or the EdgesSource property changes.

The visual representation of the nodes in the graph structure can be specified using a control style that is set by means of the NodeControlStyle property. Note that the target type of the control style needs to be a NodeControl. Similarly, for the group nodes in the graph structure, the visual representation can be specified using a control style that is set by means of the GroupNodeControlStyle property. The visual style for the edges can be specified through the EdgeStyle property.

Tutorial Demo Code

The BusinessModelAdapterWindow demo application shows how to use the WPF data binding mechanism in conjunction with a data model that does not fit the specific use-cases for which the yFiles WPF data binding support classes are tailored.