gpt4 book ai didi

wpf - 如何使用依赖属性在两个 WPF 控件之间画一条线?

转载 作者:行者123 更新时间:2023-12-04 15:52:55 26 4
gpt4 key购买 nike

我需要画一个连接线两个 WPF 控件。我定义了一个 依赖属性 在我的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(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
};
}
}

最佳答案

要使您的示例正常工作,您只需发送到 绑定(bind) conn.Source.Destination到节点的 AnchorPoints,否则连接器只获取 AnchorPoints 的初始值 (0,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);

关于wpf - 如何使用依赖属性在两个 WPF 控件之间画一条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16776829/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com