c# - 使用图形的图形布局#

标签 c# .net wpf graph graph-sharp

这是我的窗口代码:

public partial class MainWindow
{
    private MainWindowViewModel _mainWindowViewModel;

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

和 View 模型代码:

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));
    }
}

ViewModelBase 类:

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}" />

问题是我在这个布局中看不到任何东西。也许我以错误的方式绑定(bind)数据? 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

关于c# - 使用图形的图形布局#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6020382/

相关文章:

c# - Application.Current 为空,但 new Application() 仍然失败?

wpf - 在 Canvas 上的 DataBound 项目中设置 Z-Index

c# - 使用 Linq 检查日期在范围内

c# - 排除通用约束中的类型(可能?)

c# - 如果实例已被处置,则在不调用 EndXXX 的情况下调用 BeginXXX 是否安全

c# - Response.Redirect 不起作用

c# - 在 2 个 .NET 程序集中使用相同名称和命名空间的类型

c# - 为什么不能在泛型中要求运算符重载

c# - 无法加载文件或程序集 'LinqToExcel,版本

c# - 管理双屏最大化功能