Interact with Edge

Mar 27, 2012 at 10:24 AM


Graphviz4Net is excellent and powerfull, but it's not possible to know which path is what Edge.

I need to click on a Path and retrieve the Edge associated with it.

Do you have an idee how can I do that ?





Mar 28, 2012 at 8:41 PM

Hi Bruno,

take a look at the example, there is a DataTemplate for edge, it's a DataTemplate for type EdgeViewModel and each object of type EdgeViewModel has property Edge, which gives you the original object representing the edge. 

I will try to explain it here little bit more:

Firstly, you need to set property UseContentPresenterForAllElements of the GraphLayout to true, like in this code:

<WPF:GraphLayout UseContentPresenterForAllElements="True" Graph="{Binding Graph}"/>

Then you should define DataTemplate for the EdgeViewModel type, where you can use the Edge property. The Edge property returns the original edge object, which can be of any type you use in your graph, you can use predefined classes, or you can define your class, it only has to implement IEdge, however, extending one of the already provided classes will be more convenient. Your implementation of an edge can contain any properties you like and it can also contain commands, this way you can execute some code in response to, for example, a click on the edge. Details of this approach are more about Model-View-ViewModel pattern, which you can find loads of information about on Google. The implementation of the DataTemplate for the EdgeViewModel from the example looks like this:       

<DataTemplate DataType="{x:Type ViewModels:EdgeViewModel}">
  <Path Data="{Binding Data}" Stroke="Black" StrokeThickness="1" ToolTip="{Binding Edge.Label}"/>

Hope this helps,