猿问

如何在 XAML 中更改 GraphSharp 顶点的外观

我在需要更改顶点外观的项目中使用 GraphSharp。我试图创建一个自定义顶点类,它只有一个名为 Name 的属性。然后我创建了一个 ViewModel 类,我在其中创建了顶点和边。为了呈现此图,我为我的自定义顶点创建了一个 DataTemplate。代码如下:-


class MyVertex

{

    public string Name { get; set; }

}


class MainViewModel : INotifyPropertyChanged

{

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged([CallerMemberName]string name = "")

    {

        if (PropertyChanged == null) return;

        PropertyChanged(this, new PropertyChangedEventArgs(name));

    }


    public IBidirectionalGraph<MyVertex, IEdge<MyVertex>> Graph { get; private set; }


    public void CreateGraphToVisualize()

    {

        var g = new BidirectionalGraph<MyVertex, IEdge<MyVertex>>();


        //add the vertices to the graph

        MyVertex[] vertices = new MyVertex[5];

        for (int i = 0; i < 5; i++)

        {

            vertices[i] = new MyVertex { Name = "Vertex " + i.ToString() };

            g.AddVertex(vertices[i]);

        }


        //add some edges to the graph

        g.AddEdge(new Edge<MyVertex>(vertices[0], vertices[1]));

        g.AddEdge(new Edge<MyVertex>(vertices[1], vertices[2]));

        g.AddEdge(new Edge<MyVertex>(vertices[2], vertices[3]));

        g.AddEdge(new Edge<MyVertex>(vertices[3], vertices[1]));

        g.AddEdge(new Edge<MyVertex>(vertices[1], vertices[4]));


        Graph = g;

    }


    public MainViewModel()

    {

        CreateGraphToVisualize();

    }

}


但是当我运行它时,我只得到缩放控制但没有图表。所以,我想我做错了什么。然后我找到了这个并创建了一个新项目并复制了那里提供的代码。我必须根据自己的判断来判断 DataTemplate 的放置位置,所以我将它放在 Window.Resources 块中,就像上面的代码一样。我还对代码进行了一些更改以使用通用类,因为站点中使用的类显然不可用。但最终结果与我自己的代码相同。没有图表。我在这里错过了什么吗?在此先感谢您的帮助。


素胚勾勒不出你
浏览 101回答 1
1回答

SMILET

如果没有正确的类型组合,则无法实例化 GraphLayout。添加如下内容:public class MyGraph : BidirectionalGraph<MyVertex, IEdge<MyVertex>>{}public class MyGraphLayout : GraphLayout<MyVertex, IEdge<MyVertex>,MyGraph>&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; };然后使用定义的 MyGraphLayout 代替。<local:MyGraphLayout x:Name="graphLayout"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Graph="{Binding Graph}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LayoutAlgorithmType="ISOM"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OverlapRemovalAlgorithmType="FSA"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HighlightAlgorithmType="Simple"/>
随时随地看视频慕课网APP
我要回答