5

这是我的窗口代码:

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

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

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

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

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

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

1 回答 1

2
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
于 2011-05-16T16:35:01.430 回答