Events on Graph Visualization

Jun 12, 2009 at 1:33 PM

Hey guys!

I finally managed to get the graph visualized in Windows Forms, but now I was wondering if I coud add events to the graphical representation, like for example, that when I clicl a vertex, a messagebox pops up. How should I apporach this, since the Graph object doesnt seem to have event handling?

Jun 17, 2009 at 9:15 PM
Edited Jun 17, 2009 at 9:16 PM

Is pretty much easy to add event to Vertex/Edge Control.

VertexContro and EdgeControl derive (not directly) from UIElement. So they implement a lot of pre-configured Event.

To attach  your code to an event, you could use the DataTemplate features of WPF.

For example you can add this little piece of code inside the XAML windows that contain your Graph layout.

    <DataTemplate DataType="{x:Type Sample:Vertex}">

        <TextBlock MouseLeftButtonDown="Event_handler">

            TextBlock Content




Sample is the namespace where is coded the Vertex control

Event_handler is the name of the event that you have to implement into the code behind cs/vb file.


Jun 28, 2009 at 2:00 PM

and what is the namespace of the Vertex control? I thought it was Graphsharp.Controls, but this doesnt work...

<UserControl x:Class="PFCEditor.Diagram"
             Height="300" Width="300"
            <graphsharp:GraphLayout x:Name="graphLayout"
                                    Graph="{Binding ElementName=root,Path=GraphToVisualize}"
                                    OverlapRemovalAlgorithmType="FSA" />
        <DataTemplate DataType="{x:Type graphsharp:Vertex}">
            <TextBlock MouseLeftButtonDown="TextBlock_MouseLeftButtonDown" />

Or maybe you meant...? (But it doesnt work either :( )

        <DataTemplate DataType="{x:Type graphsharp:VertexControl}">

Jun 28, 2009 at 8:11 PM

in your case i think you should use the 'state' class (based on the other discussion)

Jun 28, 2009 at 9:25 PM
Edited Jun 28, 2009 at 11:48 PM

Exactly! The code that worked was this

        <DataTemplate DataType="{x:Type PFCEditor:State}">
<TextBlock MouseLeftButtonDown="TextBlock_MouseLeftButtonDown" />

But not I find another problem: when I raise the event, the "sender" is identified as a TextBlock, and I need to access to the state stored in that VertexControl.


        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)


How could I achieve that? :(

Jun 29, 2009 at 8:51 AM

That's rather a WPF question than a Graph# question, but...

There's 2 simple solution:

  1. TextBlock.DataContext or
  2. Bind it to the TextBlock's Tag property with
    <TextBlock ... Tag="{Binding}" />
    this, so you can access it through the TextBlock.Tag


Jul 16, 2009 at 8:26 AM
Edited Jul 16, 2009 at 8:28 AM


i would like to do nearly the same thing, i.e. showing a contextual menu when right-clicking a vertex.

I use a simple BidirectionalGraph<string, IEdge<string>> but i can't figure out what to put in my datatemplate's datatype.

I've tried the following:

DataTemplate DataType="{x:Type QuickGraph:TVertex}">

<TextBlock MouseRightButtonDown="MouseRightClick"/>





<DataTemplate DataType="String">

but these don't work.

How can I do that?