Reset Graph throws an exception

Aug 15, 2012 at 2:39 PM

Hello,

i have a problem to reset the View of my Graph.

i wrote a function to display only the way from leafs to root. That works fine but when i want to reset the graph to display all vertices and edges after that, throws the function graphLayout.Relayout() an exception.

Exception: KeyNotFoundException

The given key was not present in the dictionary.

Here some details:

I have an event. If you click on an vertex with ID "Receiver" the will reduce the graph to display only the selected vertex and his roots. If you click again the graph will be reset.

        public void VertexDoubleClick(object sender, MouseEventArgs e)
        {
            VertexControl tmp = null;
            if (sender.GetType().ToString() == "GraphSharp.Controls.VertexControl")
            {
                tmp = (VertexControl)sender;
            }
            if (((MyVertex)tmp.Vertex).ID.Contains("Receiver"))
            {
                vm.DrawLeaftoRoot((MyVertex)tmp.Vertex);
                graphLayout.Relayout(); //Exception throws here
            }
        }

The Function DrawLeaftoRoot checks if the complete graph is displayed. If not the graph will be reset by the function ResetView

public void DrawLeaftoRoot(MyVertex source)
        {
            if (completeview)
            {  

                DisplaySubtree(MyVertex source);        

            }
            else
            {
                ResetView();
            }

        }

public void ResetView()
        {
            //Reset
            for (int i = 0; i < vertices.Count; i++)
            {
                graph.AddVertex(vertices[i]);
            }
            for (int i = 0; i < edges.Count; i++)
            {
                if (!graph.Edges.Contains(edges[i]))
                {
                    graph.AddEdge(edges[i]);
                }
            }
            completeview = true;
        }

If i remove graphLayout.Relayout() there are no exceptions but the layout is not right. First the order of the vertices was completely different than the vertex-order which I give into it. And second the layout looks after that pretty random.

I think the Relayout() have problems with the recently added vertices and edges, and it looks like that the add-funktions of them take a bit time to finish.

Had someone the same problem with the Relayout(). Or is there another option to reset the graph?

Aug 20, 2012 at 9:11 AM

I've tried to reinitialize my ViewModel and set the DataContext of my GraphControl and it works great.

 public void VertexDoubleClick(object sender, MouseEventArgs e)
        {
            VertexControl tmp = null;
            if (sender.GetType().ToString() == "GraphSharp.Controls.VertexControl")
            {
                tmp = (VertexControl)sender;
            }
            if (((MyVertex)tmp.Vertex).ID.Contains("Receiver"))
            {
                if (isComplete)
                {
                    this.DrawLeaftoRoot((MyVertex)tmp.Vertex);
                    isComplete = false;
                }
                else
                {
                    this.DataContext = vm = new MainWindowViewModel(vertexlist, edgelist); //Reset the Graph
                    isComplete = true;
                }
            }
        }