2012-05-09 12 views
0

我在Canvas及其座標中有兩個UIElements(即矩形)。我如何在後面的代碼中將它們與弧連接起來?將兩個UIElement連接到Arc

+0

我使用谷歌,我找不到anythin有用。我試圖創建ArcSegment但沒有任何成功。如果有:'ArcSegment arc = new ArcSegment(x1,y1,x2,y2);' – Bip

回答

2

不需要在矩形(或其他對象)上得到精確的命中:確保Z排序是正確的。 arc.SetValue(Canvas.ZIndex, -1)會將其推到背景。如果你想要一個垂直命中,你需要打破代數:/

對於弧:(見http://msdn.microsoft.com/en-us/library/ms751808.aspx),它需要被包含在一個PathFigure中。

編輯:這顯示了兩個連接的矩形。這條線簡單地在兩個中心之間運行。弧開始於一箇中心(pathFigure startpoint),第一個參數是第二個對象的中心。

 r1 = new Rectangle(); 
     r1.Margin = new Thickness(50, 50, 0, 0); 
     r1.VerticalAlignment = System.Windows.VerticalAlignment.Top; 
     r1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; 
     r1.Height = 50; 
     r1.Width= 50; 
     r1.Fill = new SolidColorBrush(Colors.Red); 


     r2 = new Rectangle(); 
     r2.Width = 50; 
     r2.Height = 50; 
     r2.Fill = new SolidColorBrush(Colors.Blue); 
     r2.Margin = new Thickness(350, 450, 0, 0); 
     r2.VerticalAlignment = System.Windows.VerticalAlignment.Top; 
     r2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; 

     l = new Line(); 
     l.X1 = 75; 
     l.Y1 = 75; 
     l.X2 = 375; 
     l.Y2 = 475; 
     l.Fill = new SolidColorBrush(Colors.Purple); 
     l.Stroke = new SolidColorBrush(Colors.Purple); 
     l.StrokeThickness = 2; 
     l.SetValue(Canvas.ZIndexProperty, -1); 

     PathGeometry myPathGeometry = new PathGeometry(); 

     // Create a figure. 
     PathFigure pathFigure1 = new PathFigure(); 
     pathFigure1.StartPoint = new Point(75, 75); 

     pathFigure1.Segments.Add(
      new ArcSegment(
       new Point(375, 475), 
       new Size(50, 50), 
       45, 
       true, /* IsLargeArc */ 
       SweepDirection.Clockwise, 
       true /* IsStroked */)); 
     myPathGeometry.Figures.Add(pathFigure1); 

     // Display the PathGeometry. 
     Path myPath = new Path(); 
     myPath.Stroke = Brushes.Black; 

     myPath.StrokeThickness = 1; 
     myPath.Data = myPathGeometry; 
     myPath.SetValue(Canvas.ZIndexProperty, -1); 

     LayoutRoot.Children.Add(r1); 
     LayoutRoot.Children.Add(r2); 
     LayoutRoot.Children.Add(l); 
     LayoutRoot.Children.Add(myPath); 
+0

如果您將線隱藏在其他兩個形狀的後面,則會非常棒。圓弧可以在兩個其他物體的中心之間運行。 z排序完成其餘部分。 – Nzc

+0

數學包含在Arc對象中。 – Nzc