Data Binding for Graph Structures

yFiles for Silverlight provides support for the Silverlight 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 for Silverlight data binding support provides a convenient way of creating a graph structure from a data source. It bases on the Silverlight 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 for Silverlight data binding support also enables specifying both the visual representation of nodes and edges in the graph and the hierarchical organization in a grouped graph. 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).

Please keep in mind that, in this case, it might be easier to abstain from using the data binding support and create the graph the "regular way", that is, 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 Silverlight data binding mechanism can be found in the Silverlight Developer Center. For example:

Generally, the Silverlight data binding mechanism is very similar to that used by Windows Presentation Foundation (WPF). Information related to the WPF data binding mechanism can be found in the .NET framework's documentation. For example:

Choosing a Suitable Data Binding Class

The yFiles for Silverlight data binding support covers some of the most common ways of creating graph structures from business data. Since each use case has its own specifics, there are several different classes, each tailored for a specific set of structure of the input data and the desired graph features. Table 4.35, “Data binding classes” lists the classes that provide data binding and their main properties.

When choosing a data binding class, a plain criterion is whether the data has tree graph structure or not. A tree is a graph without cycles, for example hierarchic data in which each object has exactly one parent (and several children) like an organizational chart. In this case, class TreeSource can be used unless graph grouping is required which it doesn't support. Of course, all classes for general graphs can be used for trees as well, and class AdjacentNodesGraphSource is even a more general version of TreeSource.

If the business data does not describe a tree structure but a general graph, one of the classes GraphSource, AdjacentNodesGraphSource, or AdjacentEdgesGraphSource can be chosen based on the way the connections between nodes are specified in your data model. If connections are specified explicitly, data can be bound to edge labels and creating multiple edges between the same pair of nodes is possible. A more detailed description of the data models required by each class is given in the following Table 4.35, “Data binding classes”.

Table 4.35. Data binding classes

Type Name Graph Structure Description
TreeSource Tree Creates a tree graph from a data model without explicit objects for "connections" (edges). Each business object specifies its child objects and thereby, edges are defined implicitly. Supports binding data to an edge's label. See more details below.
GraphSource General Creates a general graph from a data model which provides business objects for both nodes and edges. The collection of nodes and the collection of edges are specified separately. Supports binding data to an edge's label and also multiple edges between the same pair of node objects. See more details below.
AdjacentNodesGraphSource General Creates a general graph from a data model without explicit objects for "connections" (edges). Each business object specifies the objects in its neighborhood and thereby, edges are defined implicitly. See more details below.
AdjacentEdgesGraphSource General Creates a general graph from a data model which contains business objects for both nodes and edges. Each business object specifies its incoming and outgoing connections. Supports binding data to an edge's label and also multiple edges between the same pair of node objects. See more details below.

Class TreeSource

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

TreeSource supports the creation of a set of tree structures that are part of the same graph, that means, the created graph structure can be actually a "forest".

The creation of trees with a hierarchical organization into group nodes is not supported by this class. Instead, class AdjacentNodesGraphSource can be used for this task. In fact, it is a more general version of TreeSource and can be used as a drop-in replacement for TreeSource by setting the same collection as NodesSource and using the binding SuccessorsBinding instead of ChildBinding (with the same business objects).

Note

The differences of the classes of the yFiles for Silverlight data binding support and their specific advantages and disadvantages are discussed at the beginning of this topic in the section called “Choosing a Suitable Data Binding Class”.

Binding Properties

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

  • NodesSource denotes the collection of business objects from the data model for which nodes will be created. At least, this collection must contain the objects for which root nodes should be created. Specifying business objects that correspond to inner nodes and even specifying all business objects is supported as well.
  • ChildBinding denotes the binding to get the collection of child business objects of a business object for which a node was created already. A corresponding node will be created for each new object in the collection and, subsequently, edges will be created that connect the node corresponding to the initial business object to each of the nodes corresponding to the child business objects.

A binding that allows specifying the business objects for edge labels is provided through the property EdgeLabelBinding.

Additional binding properties and other features of class TreeSource that are common to all classes of the yFiles for Silverlight data binding support are described in the section called “Common Features”.

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 the 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 the EdgeLabelBinding corresponds to a child node in the graph, that is, to the target node of the respective edge.

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

Example 4.53. XAML definition of a TreeSource data binding

<y:TreeSource ...
    NodesSource="{Binding Source={StaticResource customerRepository}, 
                  Path=Customers}" 
    ChildBinding="{Binding Path=RelatedCustomers}" 
    ...
>

Class GraphSource

The class GraphSource enables a data binding to create a general graph structure from a data model. The data model needs to provide the business objects for both the nodes and the edges and bindings that specify for each edge both of its end nodes.

Additionally, GraphSource supports a data binding that creates a hierarchical organization in a grouped graph. For this, the data model needs to provide the business objects for the group nodes and to associate each group with the (regular and group) nodes it contains.

Note

The differences of the classes of the yFiles for Silverlight data binding support and their specific advantages and disadvantages are discussed at the beginning of this topic in the section called “Choosing a Suitable Data Binding Class”.

Binding Properties

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

  • 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 which edges will be created subject to the results from the following two bindings.
  • SourceNodeBinding and TargetNodeBinding denote the bindings to get from the business object for an edge to the business objects for its source node and target node, respectively.

A binding that allows specifying the business objects for edge labels is provided through the property EdgeLabelBinding.

Additional binding properties, most notably to create a hierarchical organization, and other features of class GraphSource that are common to all classes of the yFiles for Silverlight data binding support are described in the section called “Common Features”.

Building the Graph Structure

GraphSource builds the graph structure using the collections returned by the properties NodesSource and EdgesSource and the bindings denoted by the properties 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.

If the boolean property LazyNodeDefinition is set to false, it is crucial that each of the bindings evaluates to a business object that is contained in the NodesSource collection, otherwise no edge will be created for a given business object of the collection returned by the EdgesSource property. If LazyNodeDefinition is set to true, a new node is associated with each business object that is not contained in the NodesSource collection, and an edge is created for every business object of the EdgesSource collection.

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 the EdgeLabelBinding also corresponds to an edge in the graph.

Class AdjacentNodesGraphSource

The class AdjacentNodesGraphSource enables a data binding to create a general graph structure from a data model. The data model needs to provide the business objects for the nodes and bindings that specify for each business object a collection of its predecessors and/or a collection of its successors.

Additionally, GraphSource supports a data binding that creates a hierarchical organization in a grouped graph. For this, the data model needs to provide the business objects for the group nodes and to associate each group with the (regular and group) nodes it contains.

This class is a more general version of class TreeSource and can be used as a drop-in replacement for it by setting the same collection as NodesSource and using the binding SuccessorsBinding instead of ChildBinding (with the same business objects).

Note

The differences of the classes of the yFiles for Silverlight data binding support and their specific advantages and disadvantages are discussed at the beginning of this topic in the section called “Choosing a Suitable Data Binding Class”.

Binding Properties

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

  • NodesSource denotes the business objects from the data model for which initial nodes will be created. This collection must contain at least one business object from which all other business objects are reachable by iteratively following the connections specified by the PredecessorsBinding and SuccessorsBinding. Specifying more and even specifying all business objects is supported as well.
  • PredecessorsBinding and SuccessorsBinding denote the bindings to get the collections of predecessor and successor business objects of a given business object. A corresponding node will be created for each new object in the collection and, subsequently, edges will be created that connect the node corresponding to the given business object to each of the nodes corresponding to the predecessor and successor business objects.

Additional binding properties, most notably to create a hierarchical organization, and other features of class AdjacentNodesGraphSource that are common to all classes of the yFiles for Silverlight data binding support are described in the section called “Common Features”.

Building the Graph Structure

AdjacentNodesGraphSource builds the graph structure iteratively using the collection returned by the property NodesSource and the bindings denoted by the PredecessorsBinding and SuccessorsBinding properties in the following way. First, the initial set of nodes is created for business objects of the collection returned by the NodesSource property. Then, iteratively, the predecessor and successor bindings are evaluated for already created nodes to determine their neighborhood, additional nodes are created for new business objects if required, and finally, the corresponding edges are created.

During the iterative process, the data context for the PredecessorsBinding and the SuccessorsBinding is always a business object from the data model that corresponds to a node in the graph.

Class AdjacentEdgesGraphSource

The class AdjacentEdgesGraphSource enables a data binding to create a general graph structure from a data model. The data model needs to provide the business objects for the nodes and bindings that specify for each business object a collection of its incoming connections and/or a collection of its outgoing connections. Additionally, bindings that specify for each incoming edge its source node and for each outgoing edge its target node are required.

Additionally, GraphSource supports a data binding that creates a hierarchical organization in a grouped graph. For this, the data model needs to provide the business objects for the group nodes and to associate each group with the (regular and group) nodes it contains.

Note

The differences of the classes of the yFiles for Silverlight data binding support and their specific advantages and disadvantages are discussed at the beginning of this topic in the section called “Choosing a Suitable Data Binding Class”.

Binding Properties

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

  • NodesSource denotes the business objects from the data model for which initial nodes will be created. This collection must contain at least one business object from which all other business objects are reachable by iteratively following the connections specified by the InEdgesBinding and OutEdgesBinding. Specifying more and even specifying all business objects is supported as well.
  • InEdgesBinding and OutEdgesBinding denote the bindings to get for the given business object the collections of business objects which correspond to incoming and outgoing edges, respectively. Edges will be created subject to the results from the following two bindings.
  • SourceNodeBinding and TargetNodeBinding denote the bindings to get from the business object for an edge the business objects for its source node and target node, respectively. Since the target node of an incoming edge is already defined by the business object in which the binding is evaluated, the TargetNodeBinding is used only for outgoing edges. Similarly, the SourceNodeBinding is used only for incoming edges. Note that a corresponding node will be created for each new source (target) business object.

A binding that allows specifying the business objects for edge labels is provided through the property EdgeLabelBinding.

Additional binding properties, most notably to create a hierarchical organization, and other features of class AdjacentEdgesGraphSource that are common to all classes of the yFiles for Silverlight data binding support are described in the section called “Common Features”.

Building the Graph Structure

AdjacentEdgesGraphSource builds the graph structure iteratively using the collection returned by the property NodesSource and the bindings that define edges in the following way. First, the initial set of nodes is created for business objects of the collection returned by the NodesSource property. Then, iteratively, the in-edges and out-edges bindings are evaluated for already created nodes to determine their connections, additional nodes are created for new business objects if required, and finally, the corresponding edges are created.

During the iterative process, the data context for the InEdgesBinding (OutEdgesBinding) is always the business object from the data model that corresponds to a node in the graph that is the target (source) for the corresponding edge. 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 the EdgeLabelBinding also corresponds to an edge in the graph.

Common Features

The features described in this section are supported by all classes of the yFiles for Silverlight data binding support.

The property DynamicUpdates sets the dynamic behavior of the data binding. If the collection view of the graph's nodes can be queried for changes (which is true for the default implementations), enabling the property results in the graph structure being rebuilt whenever one of the provided collections of business objects, for example NodesSource, or one of the bindings 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 property NodeControlStyle. 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 property EdgeStyle.

Similarly, for classes that support the creation of a hierarchical organization, the visual representation for the group nodes in the graph structure can be specified using a control style that is set by means of the property GroupNodeControlStyle.

The visual appearance of the graph elements can also be determined by setting a style in the node defaults, group node defaults and edge defaults. The classes that provide an edge label binding also support setting the label style using edge label defaults. Please note that styles assigned to the (group) node defaults, edge defaults, or label defaults override the control style that has eventually been set using NodeControlStyle, GroupNodeControlStyle, EdgeStyle, EdgeLabelControlStyle.

The properties LocationXBinding and LocationYBinding can be used to specify bindings that determine the x- and y-coordinates of a business object's node.

Hierarchical Organization of General Graphs

The classes that create a general graph structure, namely classes GraphSource, AdjacentNodesGraphSource, and AdjacentEdgesGraphSource, support the creation of a hierarchical organization using the following 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 (group) node to the business object for its containing group node.

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 as the parent group of the (group) node which corresponds to the business object that is used as the binding's data context.

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

Tutorial Demo Code

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