Tôi đang làm việc trên một dự án liên quan đến việc vẽ đường cong cong giữa hai đối tượng. Hiện tại, tôi đã viết một số mã thử nghiệm để chơi xung quanh với các đường cong và hoạt hình bezier. Thử nghiệm đầu tiên đơn giản là di chuyển điểm cuối (Point3) từ đối tượng gốc (hình chữ nhật) đến đối tượng đích (hình chữ nhật khác), theo một đường thẳng. đây là mã thiết lập dòng thực tế:WPF Animation - Điểm động Bezier đường cong
connector = new Path();
connector.Stroke = Brushes.Red;
connector.StrokeThickness = 3;
PathGeometry connectorGeometry = new PathGeometry();
PathFigure connectorPoints = new PathFigure();
connectorCurve = new BezierSegment();
connectorPoints.StartPoint = new Point((double)_rect1.GetValue(Canvas.LeftProperty) + _rect1.Width/2,
(double)_rect1.GetValue(Canvas.TopProperty) + _rect1.Height/2);
connectorCurve.Point1 = connectorPoints.StartPoint;
connectorCurve.Point2 = connectorPoints.StartPoint;
connectorCurve.Point3 = connectorPoints.StartPoint;
connectorPoints.Segments.Add(connectorCurve);
connectorGeometry.Figures.Add(connectorPoints);
connector.Data = connectorGeometry;
MainCanvas.Children.Add(connector);
OK, vì vậy hiện tại chúng tôi có một đường bị thu hẹp thành một điểm. Bây giờ, hãy tạo hiệu ứng cho dòng đó, chuyển từ _rect1 sang _rect2 (hai đối tượng tại điểm cuối):
PointAnimation pointAnim = new PointAnimation();
pointAnim.From = connectorCurve.Point3;
pointAnim.To = new Point((double)_rect2.GetValue(Canvas.LeftProperty) + _rect2.Width/2,
(double)_rect2.GetValue(Canvas.TopProperty) + _rect2.Height/2);
pointAnim.Duration = new Duration(TimeSpan.FromSeconds(5));
board.Children.Add(pointAnim);
Hoạt động đẹp mắt. Tuy nhiên, khi tôi cố gắng làm điều đó với một bảng phân cảnh, tôi chẳng nhận được gì cả. Dưới đây là mã kịch bản:
Storyboard board = new Storyboard();
PointAnimation pointAnim = new PointAnimation();
pointAnim.From = connectorCurve.Point3;
pointAnim.To = new Point((double)_rect2.GetValue(Canvas.LeftProperty) + _rect2.Width/2,
(double)_rect2.GetValue(Canvas.TopProperty) + _rect2.Height/2);
pointAnim.Duration = new Duration(TimeSpan.FromSeconds(5));
Storyboard.SetTarget(pointAnim, connectorCurve);
Storyboard.SetTargetProperty(pointAnim, new PropertyPath(BezierSegment.Point3Property));
board.Children.Add(pointAnim);
board.Begin();
Không có gì di chuyển. Tôi nghi ngờ có một vấn đề với những gì tôi đang cho ăn SetTarget hoặc SetTargetProperty, nhưng dường như không thể tìm ra nó. Có ai có kinh nghiệm với dòng động/bezier điểm trong WPF?
Tôi không chắc chắn rằng sẽ giải quyết được vấn đề của bạn, nhưng bạn có thể tìm thấy một số nguồn cảm hứng trong bài đăng này: http://www.japf.fr/?p=227 –