Error trying to handle VertexClick event

May 22, 2013 at 9:20 AM

In a WPF project we are working with groups and we want to collapse them easily by clicking. So far it works using the following code:
private void nodeXLControl_VertexClick(object sender, VertexEventArgs vertexEventArgs)
    var groups = GroupUtil.GetGroups(nodeXLControl.Graph);
    var dict = GroupUtil.GetGroupIndexDictionary(groups);
    int groupID = -1;
    dict.TryGetValue(vertexEventArgs.Vertex.ID, out groupID);
    GroupInfo group = (GroupInfo) groups.GetValue(groupID);
    nodeXLControl.CollapseGroup(group.Name, true);
When binded to hovering it works, but when the event is binded to the VertexClick event, it throws an exception:
Smrf.NodeXL.Visualization.Wpf.VertexDrawer.TryDrawVertex: The vertex doesn't belong to a graph. It can't be drawn.
This is probably because NodeXL tries to select the vertex after it has collapsed it, so essentially removing it from the graph. Is there a way to stop the propagation of the event further on to NodeXL, or to change the default behavior?
May 22, 2013 at 3:24 PM
Edited May 22, 2013 at 5:10 PM
Your guess is correct. Unfortunately, I can't think of a way to change the default behavior of the VertexClick event without modifying and recompiling the NodeXL source code.

A possible workaround is to handle the GraphMouseUp event instead of VertexClick. In my brief glance at the source code (NodeXLControl.OnMouseUp(), which is at as of today), it doesn't look like the vertex will be redrawn in the default code. I'm not sure about that, but it might be worth a try. Of course, you would then get your group collapse on mouse up instead of mouse down.

Another option is to display a context menu in a GraphMouseUp handler and show a "Collapse Group" item in your menu.

-- Tony