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

时间:2019-06-30 12:46:29

标签: c# wpf xaml mvvm graph-sharp

我在我的项目中使用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();
    }
}

<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块中,就像上面的代码一样。我还对代码进行了一些更改以使用通用类,因为该站点中使用的通用类显然不可用。但是最终结果与我自己的代码相同。没有图。 我在这里想念什么吗? 预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

没有正确的类型组合就无法实例化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"/>