2013-05-27 21 views
5

Tôi cần vẽ một đường kết nối hai điều khiển WPF. Tôi đã định nghĩa một thuộc tính phụ thuộc phụ thuộc trong các đối tượng Node của mình vì vậy nếu Nút được di chuyển, đường thẳng vẫn kết nối các đối tượng.Làm thế nào để vẽ một đường thẳng giữa hai điều khiển WPF bằng cách sử dụng các thuộc tính phụ thuộc?

Tôi có ví dụ sau nhưng tôi không thể làm cho nó hoạt động.

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     Node node1 = new Node(myCanvas) { Width = 50, Height = 50 }; 
     Node node2 = new Node(myCanvas) { Width = 50, Height = 50 }; 

     Canvas.SetLeft(node1, 0); 
     Canvas.SetLeft(node2, 200); 

     Canvas.SetTop(node1, 0); 
     Canvas.SetTop(node2, 0); 

     myCanvas.Children.Add(node1); 
     myCanvas.Children.Add(node2); 

     Connector conn = new Connector(); 
     conn.Source = node1.AnchorPoint; 
     conn.Destination = node2.AnchorPoint; 

     myCanvas.Children.Add(conn); 
    } 
} 

class Node : Control 
{ 
    public static readonly DependencyProperty AnchorPointProperty = 
     DependencyProperty.Register(
      "AnchorPoint", typeof(Point), typeof(Node), 
       new FrameworkPropertyMetadata(new Point(0, 0), 
       FrameworkPropertyMetadataOptions.AffectsMeasure)); 

    public Point AnchorPoint 
    { 
     get { return (Point)GetValue(AnchorPointProperty); } 
     set { SetValue(AnchorPointProperty, value); } 
    } 

    private Canvas mCanvas; 

    public Node(Canvas canvas) 
    { 
     mCanvas = canvas; 
     this.LayoutUpdated += Node_LayoutUpdated; 
    } 

    void Node_LayoutUpdated(object sender, EventArgs e) 
    { 
     Size size = RenderSize; 
     Point ofs = new Point(size.Width/2, size.Height/2); 
     AnchorPoint = TransformToVisual(this.mCanvas).Transform(ofs); 
    } 

    protected override void OnRender(DrawingContext drawingContext) 
    { 
     drawingContext.DrawEllipse(
      Brushes.Red, 
      null, 
      new Point(Width/2, Height/2), Width/2, Height/2); 
    } 
} 

public sealed class Connector : UserControl 
{ 
    public static readonly DependencyProperty SourceProperty = 
     DependencyProperty.Register(
      "Source", typeof(Point), typeof(Connector), 
      new FrameworkPropertyMetadata(default(Point))); 

    public Point Source { 
     get { return (Point)this.GetValue(SourceProperty); } 
     set { this.SetValue(SourceProperty, value); } 
    } 

    public static readonly DependencyProperty DestinationProperty = 
     DependencyProperty.Register(
      "Destination", typeof(Point), typeof(Connector), 
       new FrameworkPropertyMetadata(default(Point))); 

    public Point Destination { 
     get { return (Point)this.GetValue(DestinationProperty); } 
     set { this.SetValue(DestinationProperty, value); } 
    } 

    public Connector() 
    { 
     LineSegment segment = new LineSegment(default(Point), true); 
     PathFigure figure = new PathFigure(default(Point), new[] { segment }, false); 
     PathGeometry geometry = new PathGeometry(new[] { figure }); 
     BindingBase sourceBinding = 
      new Binding { Source = this, Path = new PropertyPath(SourceProperty) }; 
     BindingBase destinationBinding = 
      new Binding { Source = this, Path = new PropertyPath(DestinationProperty) }; 
     BindingOperations.SetBinding(
      figure, PathFigure.StartPointProperty, sourceBinding); 
     BindingOperations.SetBinding(
      segment, LineSegment.PointProperty, destinationBinding); 
     Content = new Path 
     { 
      Data = geometry, 
      StrokeThickness = 5, 
      Stroke = Brushes.White, 
      MinWidth = 1, 
      MinHeight = 1 
     }; 
    } 
} 
+0

Bạn có thấy [Trình chỉnh sửa nút của tôi] (http://stackoverflow.com/a/15580293/643085) không? Nó hoàn toàn MVVM và thực hiện cách PHẢI trong WPF. Bạn không cần bất kỳ DP nào để làm những gì bạn đang tìm kiếm. Ngoài ra, nếu bạn cần nhiều khả năng nâng cao hơn (chẳng hạn như Anchor Point, bạn có thể xem [Mẫu thứ hai] của tôi (http://stackoverflow.com/a/15821573/643085) –

+0

Trình kết nối đến từ đâu? – soandos

+0

Điều gì là bool isInput trong constructor Node (Canvas canvas, bool isInput)? – KMC

Trả lời

4

Tất cả bạn phải làm gì để làm cho công việc ví dụ của bạn là để bindconn.Source.Destination đến các hạch AnchorPoints, nếu không Connector chỉ nhận được AnchorPoints' giá trị ban đầu (0,0), và không nghe thêm các thay đổi:

... 

Connector conn = new Connector(); 
//conn.Source = node1.AnchorPoint; 
conn.SetBinding(Connector.SourceProperty, 
       new Binding() 
       { 
        Source = node1, 
        Path = new PropertyPath(Node.AnchorPointProperty) 
       }); 
//conn.Destination = node2.AnchorPoint; 
conn.SetBinding(Connector.DestinationProperty, 
       new Binding() 
       { 
        Source = node2, 
        Path = new PropertyPath(Node.AnchorPointProperty) 
       }); 

myCanvas.Children.Add(conn); 
Các vấn đề liên quan