C# Can';除非在InitializeComponent方法之前调用该方法,否则不要使用graphsharp绘制wpf

C# Can';除非在InitializeComponent方法之前调用该方法,否则不要使用graphsharp绘制wpf,c#,wpf,C#,Wpf,我跟随视频教程学习如何将graph#library与WPF结合使用,很容易找到一些顶点和连接它们的边。此图形的代码是在初始化组件方法之前在主窗口方法中调用的方法中编写的,因此在编译时,图形会自动显示。 问题是,我试图在按钮中调用相同的绘图方法,但每次单击按钮时都没有显示任何内容 这是我的密码 public partial class MainWindow : Window { private IBidirectionalGraph<object, IEdge<object&g

我跟随视频教程学习如何将graph#library与WPF结合使用,很容易找到一些顶点和连接它们的边。此图形的代码是在
初始化组件
方法之前在
主窗口
方法中调用的方法中编写的,因此在编译时,图形会自动显示。 问题是,我试图在
按钮中调用相同的绘图方法,但每次单击按钮时都没有显示任何内容

这是我的密码

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();
    }
}
公共部分类主窗口:窗口
{
专用IBI方向图_graphToVisualize;
公共方向图图形可视化
{
获取{return}
}
公共主窗口()
{
//CreateGraphToVisualize();//在未注释此指令的情况下编译时,将绘制图形
初始化组件();
}
私有void createGraphToVisualization()
{
var g=新的双向图();
//将顶点添加到图形中
字符串[]顶点=新字符串[5];
对于(int i=0;i<5;i++)
{
顶点[i]=i.ToString();
g、 AddVertex(顶点[i]);
}
//将边添加到图形中
g、 AddEdge(新边(顶点[0],顶点[1]);
g、 AddEdge(新边(顶点[1],顶点[2]);
g、 AddEdge(新边(顶点[2],顶点[3]);
g、 AddEdge(新边(顶点[3],顶点[1]);
g、 AddEdge(新边(顶点[1],顶点[4]);
_graphToVisualize=g;
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
CreateGraphToVisualize();
}
}

}您的问题是,窗口使用了图形可视化的绑定

<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;
    }
}
public分部类主窗口:窗口,INotifyPropertyChanged
{
专用IBI方向图_graphToVisualize;
公共方向图图形可视化{
获取{返回此。\u graphToVisualize;}
设置{
如果(!等于(值,此._图形可视化)){
这个。_graphToVisualize=值;
此。RaisePropChanged(“图形可视化”);
}
}
}
公共事件属性更改事件处理程序属性更改;
public void RaisePropChanged(字符串名称){
var eh=this.PropertyChanged;
如果(eh!=null){
呃(此,新物业变更德文塔格(名称));;
}
}
私有void createGraphToVisualization()
{
var g=新的双向图();
//将顶点添加到图形中
字符串[]顶点=新字符串[5];
对于(int i=0;i<5;i++)
{
顶点[i]=i.ToString();
g、 AddVertex(顶点[i]);
}
//将边添加到图形中
g、 AddEdge(新边(顶点[0],顶点[1]);
g、 AddEdge(新边(顶点[1],顶点[2]);
g、 AddEdge(新边(顶点[2],顶点[3]);
g、 AddEdge(新边(顶点[3],顶点[1]);
g、 AddEdge(新边(顶点[1],顶点[4]);
GraphToVisualize=g;
}
}

希望这对您有所帮助您的问题是,窗口使用了图形可视化的绑定

<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;
    }
}
public分部类主窗口:窗口,INotifyPropertyChanged
{
专用IBI方向图_graphToVisualize;
公共方向图图形可视化{
获取{返回此。\u graphToVisualize;}
设置{
如果(!等于(值,此._图形可视化)){
这个。_graphToVisualize=值;
此。RaisePropChanged(“图形可视化”);
}
}
}
公共事件属性更改事件处理程序属性更改;
public void RaisePropChanged(字符串名称){
var eh=this.PropertyChanged;
如果(eh!=null){
呃(此,新物业变更德文塔格(名称));;
}
}
私有void createGraphToVisualization()
{
var g=新的双向图();
//将顶点添加到图形中
字符串[]顶点=新字符串[5];
对于(int i=0;i<5;i++)
{
顶点[i]=i.ToString();
g、 AddVertex(顶点[i]);
}
//将边添加到图形中
g、 AddEdge(新边(顶点[0],顶点[1]);
g、 AddEdge(新边(顶点[1],顶点[2]);
g、 AddEdge(新边(顶点[2],顶点[3]);
g、 AddEdge(新边(顶点[3],顶点[1]);
g、 AddEdge(新边(顶点[1],顶点[4]);
GraphToVisualize=g;
}
}
希望这有帮助