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