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

How to Change the look of GraphSharp Vertices in XAML

我在需要更改顶点外观的项目中使用 GraphSharp。我试图创建一个自定义顶点 class,它只有一个 属性 命名为 Name。然后我创建了一个 ViewModel class,我在其中创建了顶点和边。为了呈现此图,我为我的自定义顶点创建了一个 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();
    }
}

<Window x:Class="GraphSharpBasic.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:graphsharp="clr-namespace:GraphSharp.Controls;assembly=GraphSharp.Controls"
        xmlns:zoom="clr-namespace:WPFExtensions.Controls;assembly=WPFExtensions"
        xmlns:local="clr-namespace:GraphSharpBasic"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:MyVertex}">
            <Border BorderBrush="Beige" BorderThickness="2" Background="Gainsboro" MinWidth="50" MinHeight="10">
                <TextBlock Text="{Binding Name}"/>
            </Border>
        </DataTemplate>
    </Window.Resources>
    <Window.DataContext>
        <local:MainViewModel/>
    </Window.DataContext>
    <Grid>
        <zoom:ZoomControl>
            <graphsharp:GraphLayout x:Name="graphLayout"
                           Graph="{Binding Graph}"
                            LayoutAlgorithmType="ISOM"
                            OverlapRemovalAlgorithmType="FSA"
                            HighlightAlgorithmType="Simple"/>
        </zoom:ZoomControl>
    </Grid>
</Window>

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

没有正确的类型组合就无法实例化 GraphLayout。

添加如下内容:

public class MyGraph : BidirectionalGraph<MyVertex, IEdge<MyVertex>>{}

public class MyGraphLayout : GraphLayout<MyVertex, IEdge<MyVertex>,MyGraph> 
    {
    };

然后改用定义的 MyGraphLayout。

<local:MyGraphLayout x:Name="graphLayout"
                           Graph="{Binding Graph}"
                            LayoutAlgorithmType="ISOM"
                            OverlapRemovalAlgorithmType="FSA"
                            HighlightAlgorithmType="Simple"/>