1

私はhttp://graphsharp.comのビデオ チュートリアルに従って、graph# ライブラリを 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 に答える