使用图形#的图形布局

本文关键字:图形 布局 | 更新日期: 2023-09-27 17:56:40

这是我的窗口代码:

public partial class MainWindow
{
    private MainWindowViewModel _mainWindowViewModel;
    public MainWindow()
    {
        InitializeComponent();
        _mainWindowViewModel = new MainWindowViewModel();
        DataContext = _mainWindowViewModel;
    }
}

和视图模型代码:

class MainWindowViewModel : ViewModelBase
{
    private BidirectionalGraph<string, IEdge<string>> _graph;
    public BidirectionalGraph<string, IEdge<string>> Graph
    {
        get { return _graph; }
        set
        {
            _graph = value;
            NotifyPropertyChanged("Graph");
        }
    }
    public MainWindowViewModel()
    {
        Graph = new BidirectionalGraph<string, IEdge<string>>();
        // test data
        const string vertex1 = "123";
        const string vertex2 = "456";
        const string vertex3 = "ddd";
        Graph.AddVertex(vertex1);
        Graph.AddVertex(vertex2);
        Graph.AddVertex(vertex3);
        Graph.AddEdge(new Edge<string>(vertex1, vertex2));
        Graph.AddEdge(new Edge<string>(vertex2, vertex3));
        Graph.AddEdge(new Edge<string>(vertex2, vertex1));
    }
}

视图模型基类:

class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

下面是 XAML:

<Controls:GraphLayout x:Name="graphLayout" Grid.Row="1" LayoutAlgorithmType="FR" OverlapRemovalAlgorithmType="FSA" HighlightAlgorithmType="Simple" Graph="{Binding Path=Graph}" />

问题是我在这个布局中看不到任何东西。也许我以错误的方式绑定数据?Graph# 能否与 WPF4 一起正常工作?

更新

:我已经更新了我的代码,但我仍然在图形布局中看到任何东西。

已解决:应添加自定义图形布局以正确显示图形

public class CustomGraphLayout : GraphLayout<string, IEdge<string >, BidirectionalGraph<string, IEdge<string>>> {}

使用图形#的图形布局

public BidirectionalGraph<string, IEdge<string>> Graph { get; set; }

这里没有INotifyPropertyChanged。改用这个

private BidirectionalGraph<string, IEdge<string>> _graph;
public BidirectionalGraph<string, IEdge<string>> Graph
{
    get { return _graph; }
    set
    {
        _graph = value;
        NotifyPropertyChanged("Graph");
    }
}

并确保您拥有支持INotifyPropertyChanged实现样板

public class MainWindowViewModel : INotifyPropertyChanged

#region INotifyPropertyChanged Implementation
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
}
#endregion