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>>> {}
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
精彩评论