DataBinding
The following article will show you how to bind the RadDiagram to ViewModel collections which represent nodes and edges of a Graph.
Please note that the examples in this tutorial are showcasing Telerik Windows8 theme. In the Setting a Theme article you can find more information on how to set an application-wide theme.
Defining GraphSource
In order to bind a RadDiagram successfully you have to use its GraphSource property and create a collection that implements the IGraphSource or IGraphSource<T>. The IGraphSource interface basically describes the elements (items and links (or nodes or edges)) of an MVVM graph source.
Example 1: IGraphSource interfaces
So let's start with creating a ViewModel for the nodes of our Graph (Diagram). We will create the class PascalNode that will have 'Position' property of type Point and 'PascalNumber' of type int:
Example 2: Creating a ViewModel for nodes
Now we need a ViewModel for the edges (links) of the Graph. Let's call it PascalEdge. Note that this class must implement the ILink
Example 3: Creating a ViewModel for links
Now we can successfully create an IGraphSource class to hold collection of nodes and edges:
Example 4: Implementing IGraphSource
Our next step is to create a function that accepts integer as a parameter and fills the GraphSource collection with PascalNodes and PascalEdges:
Example 5: Populating the graph source
Configuring XAML
Now let's take care of our Shapes and Connections' look and feel. We have to bind the Shapes' Position property and define ContentTemplate to show the PascalNumber. On the other hand, we have to create an empty ContentTemplate for the Connections if we don't want the default one - the name of the ViewModel's class shown in a TextBlock. We will also define a TargetCapType for the edges.
Example 6: Declaring the RadDiagram in XAML and adding the necessary styles
Connecting The View And The ViewModel
The final step is to bind the View to the ViewModels:
Example 7: Setting the GraphSource
Figure 1: Pascal Triangle with seven levels represented by RadDiagram
Two-Way MVVM
In Q2 2012 we improved the MVVM support by adding two-way changes, i.e. changes in the UI or diagram will be reflected in the viewmodel.
In order to make use of the two-way binding the data source should implement the IObservableGraphSource interface which extends the IGraphSource interface with some methods called by RadDiagram when Items are added or removed in the diagram.
The simplest way to use the IObservableGraphSource is via its default implementation in the Extensions library. The ObservableGraphSourceBase contains Add and Remove methods which can and should be overriden to make sure that any changes in the view (RadDiagram canvas), will be reflected in the ViewModel. The next examples demonstrate this in a sample scenario.
Example 8: Defining a model
Next, define a graph source deriving from the ObservableGraphSourceBase
Example 9: Implement ObservableGraphSourceBase
This model is bound to a RadTreeView and a RadDiagram in XAML as demonstrated in Example 10.
Example 10: Showing the items in a RadTreeView
Figure 2: Result from Example 10
Figure 3: Selecting an item and pasting it into the diagram
An item has been created in the MVVM model (the empty treeview item) as a result of the changes in the diagram, as should be with a two-way MVVM source. The item has however an empty content since RadDiagram has thus far no knowledge of the Person data type to insert into the model.
For this purpose you need to override the AddNode method in the Datastore as in Example 11.
Example 11: Overriding the AddNode method
Figure 4: Result from Example 11
Clearing the Cache of the ItemContainerGenerator
When an item (node) is removed from the GraphSource, its corresponding UI container (the RadDiagramShape) is stored in a collection of 'recycled' shapes for future use. This aims to speed up the diagram's performance in extensive undo-redo and container generation operations. However, this could lead to an increased use of memory. In order to clear this cache, utilize the ClearCache method of the diagram's ContainerGenerator as demonstrated in Example 12.