1

我按照http://graphsharp.com上的视频教程介绍了如何将 graph# library 与 WPF 一起使用,并且很容易找到一些顶点和连接它们的边。该图的代码写在一个方法中,该方法在该MainWindow方法之前的InitializeComponent方法中调用,因此在编译时,该图会自动出现。问题是我试图在方法中调用相同的绘图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();
    }
}

}

4

1 回答 1

4

您的问题是,该窗口使用绑定到 graphvisualize

<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 接口来解决您的问题

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;
    }
}

希望这可以帮助

于 2012-04-14T09:31:15.163 回答