Đây là mã của tôi cho cửa sổ:Graph Layout sử dụng Graph #
public partial class MainWindow
{
private MainWindowViewModel _mainWindowViewModel;
public MainWindow()
{
InitializeComponent();
_mainWindowViewModel = new MainWindowViewModel();
DataContext = _mainWindowViewModel;
}
}
Và mã xem mô hình:
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 lớp:
class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
Và ở đây đi XAML:
<Controls:GraphLayout x:Name="graphLayout" Grid.Row="1" LayoutAlgorithmType="FR" OverlapRemovalAlgorithmType="FSA" HighlightAlgorithmType="Simple" Graph="{Binding Path=Graph}" />
Vấn đề là tôi không thể nhìn thấy một thứ trong bố cục này. Có lẽ tôi ràng buộc dữ liệu theo cách sai? Graph # có hoạt động tốt với WPF4 không?
Cập nhật: Tôi đã cập nhật mã của mình, nhưng tôi vẫn không thấy gì trong bố cục biểu đồ.
Giải Quyết: Bố cục đồ tùy chỉnh sẽ được thêm vào để hiển thị đồ thị một cách chính xác
public class CustomGraphLayout : GraphLayout<string, IEdge<string >, BidirectionalGraph<string, IEdge<string>>> {}
Oh, cảm ơn. Tôi sẽ cố gắng sử dụng 'INotifyPropertyChanged', nhưng tôi có thể sử dụng' DependencyObject' và 'DependencyProperty' thay thế không? Bạn có thể cho tôi một ví dụ? Chân thành cám ơn vì câu trả lời của bạn. –
Đó là điển hình hơn trong kinh nghiệm của tôi cho viewmodels để sử dụng INPC nhưng đây là một số cuộc thảo luận về chủ đề http://stackoverflow.com/questions/291518/. Theo như mẫu đi, trang web graphsharp có một vài http://graphsharp.codeplex.com/wikipage?title=Tutorials&referringTitle=Home – kenwarner
Cảm ơn nhận xét của bạn, tôi đã cập nhật mã của mình, nhưng vẫn không có kết quả. –