除非我在InitializeComponent方法之前调用该方法,否则无法使用graphsharp绘制wpf

时间:2012-04-14 08:32:42

标签: c# wpf

我在http://graphsharp.com上关于如何在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();
    }
}

}

1 个答案:

答案 0 :(得分: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;
    }
}

希望这会有所帮助