5

Here is my code for the window:

public partial class MainWindow
{
    private MainWindowViewModel _mainWindowViewModel;

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

And the view model code:

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:

class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

And here goes the XAML:

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

The problem is that I can not see a thing in this layout. Maybe I bind data in the wrong way? Does Graph# works properly with WPF4?

Update: I have updated my code, but I still see nothing in graph layout.

Solved: Custom graph layout should be added to display graph correctly

public class CustomGraphLayout : GraphLayout<string, IEdge<string >, BidirectionalGraph<string, IEdge<string>>> {}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Kirill Dubovikov
  • 1,457
  • 2
  • 21
  • 41

1 Answers1

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

there's no INotifyPropertyChanged here. Use this instead

private BidirectionalGraph<string, IEdge<string>> _graph;

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

and make sure you have the supporting INotifyPropertyChanged implementation boilerplate

public class MainWindowViewModel : INotifyPropertyChanged

and

#region INotifyPropertyChanged Implementation

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
}

#endregion
kenwarner
  • 28,650
  • 28
  • 130
  • 173
  • Oh, thanks. I'll try to use `INotifyPropertyChanged`, but can I use `DependencyObject` and `DependencyProperty` instead? Can you give me an example? Thanks in advance for your answer. – Kirill Dubovikov May 16 '11 at 17:00
  • It's much more typical in my experience for viewmodels to use INPC but here's some discussion on the subject http://stackoverflow.com/questions/291518/. As far as samples go, the graphsharp site has a few http://graphsharp.codeplex.com/wikipage?title=Tutorials&referringTitle=Home – kenwarner May 16 '11 at 17:02
  • Thanks for your comment, I have updated my code, but there is still no result. – Kirill Dubovikov May 16 '11 at 19:29
  • 1
    your `MainWindowViewModel` constructor is calling AddVertex and AddEdge against the private field `_graph` rather than the public property `Graph`. You want to call `Graph.AddVertex(vertex1)` and so forth to ensure the `NotifyPropertyChanged` method gets called – kenwarner May 16 '11 at 19:31
  • I have figured out the problem: custom graph layout should be created to display my graph properly. – Kirill Dubovikov May 16 '11 at 20:13