我遵循了 http://graphsharp.com 上的视频教程关于如何在 WPF 中使用 graph# 库,很容易找到一些顶点和连接它们的边。该图的代码是在InitializeComponent方法之前在MainWindow方法中调用的方法中编写的,因此在编译时会自动出现该图。 问题是我试图在 button_click 方法中调用相同的绘制方法,但每次单击按钮时都没有任何显示。
这是我的代码
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();
}
}
请您参考如下方法:
你的问题是,窗口使用了到 graphvisualize 的绑定(bind)
<Window x:Class="MainWindow "
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:graphsharp="clr-namespace:GraphSharp.Controls;assembly=GraphSharp.Controls"
xmlns:zoom="clr-namespace:WPFExtensions.Controls;assembly=WPFExtensions"
Title="Window1" Height="300" Width="300" x:Name="root">
<Grid>
<zoom:ZoomControl>
<graphsharp:GraphLayout x:Name="graphLayout"
Graph="{Binding ElementName=root,Path=GraphToVisualize}"
LayoutAlgorithmType="FR" OverlapRemovalAlgorithmType="FSA"
HighlightAlgorithmType="Simple" />
</zoom:ZoomControl>
</Grid>
</Window>
使用依赖属性或使用 INotifyPropertyChanged 接口(interface)来解决你的问题
public partial class MainWindow : Window, INotifyPropertyChanged
{
private IBidirectionalGraph<object, IEdge<object>> _graphToVisualize;
public IBidirectionalGraph<object, IEdge<object>> GraphToVisualize {
get { return this._graphToVisualize; }
set {
if (!Equals(value, this._graphToVisualize)) {
this._graphToVisualize = value;
this.RaisePropChanged("GraphToVisualize");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropChanged(string name) {
var eh = this.PropertyChanged;
if (eh != null) {
eh(this, new PropertyChangedEventArgs(name));
}
}
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;
}
}
希望对你有帮助
