I followed the video tutorial on http://graphsharp.com on how to use graph# library with WPF, and it was quite easy to locate some vertices and the edges joining them. The code of this graph was written in a method which was called in the MainWindow method before the InitializeComponent method, and thus when compiling, the graph appears automatically.
The problem is that I tried to call the same method of drawing in the button_click method, but nothing appears each time I click the button.
Here’s my code
public partial class MainWindow : Window
{
private IBidirectionalGraph<object, IEdge<object>> _graphToVisualize;
public IBidirectionalGraph<object, IEdge<object>> GraphToVisualize
{
get { return _graphToVisualize; }
}
public MainWindow()
{
//CreateGraphToVisualize(); //When compiling with this instruction uncommented, the graph is drawn
InitializeComponent();
}
private void CreateGraphToVisualize()
{
var g = new BidirectionalGraph<object, IEdge<object>>();
// add the vertices to the graph
string[] vertices = new string[5];
for (int i = 0; i < 5; i++)
{
vertices[i] = i.ToString();
g.AddVertex(vertices[i]);
}
// add edges to the graph
g.AddEdge(new Edge<object>(vertices[0], vertices[1]));
g.AddEdge(new Edge<object>(vertices[1], vertices[2]));
g.AddEdge(new Edge<object>(vertices[2], vertices[3]));
g.AddEdge(new Edge<object>(vertices[3], vertices[1]));
g.AddEdge(new Edge<object>(vertices[1], vertices[4]));
_graphToVisualize = g;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
CreateGraphToVisualize();
}
}
}
your problem is, that the window use a binding to the graphvisualize
use a dependency property or use INotifyPropertyChanged interface to solve your problem
hope this helps