如何使用依赖属性在两个WPF控件之间画一条线?
发布时间:2020-05-23 19:41:00 所属栏目:程序设计 来源:互联网
导读:我需要画一条连接两个 WPF控件的线.我已经在我的Node对象中定义了一个依赖属性,所以如果移动了Node,那么该行仍然会连接这些对象. 我有以下示例,但我无法让它工作. public partial class MainWindow : Window{ public MainWindow() { InitializeComponent(); N
|
我需要画一条连接两个 WPF控件的线.我已经在我的Node对象中定义了一个依赖属性,所以如果移动了Node,那么该行仍然会连接这些对象. 我有以下示例,但我无法让它工作. 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(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",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
};
}
}
使示例工作所需要做的就是将conn.Source和.Destination绑定到节点的AnchorPoints,否则Connector只获取AnchorPoints的初始值(0,并且不会监听进一步的更改:
...
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); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
