2013-07-20 51 views
4

我正在嘗試爲我的WPF添加BezierSegment到我的畫布。我發現了一個不正確的投編譯時錯誤:將(BezierSegment添加到a)Canvas路徑

參數1:無法從「System.Windows.Media.PathGeometry」轉換爲「System.Windows.UIElement」

這是我到目前爲止...

//bezier curve it 
BezierSegment curve = new BezierSegment(startPoint, endPoint,controlPoint,false); 

// Set up the Path to insert the segments 
PathGeometry path = new PathGeometry(); 

PathFigure pathFigure = new PathFigure(); 
pathFigure.StartPoint = hs.LeStartingPoint; 
pathFigure.IsClosed = true; 
path.Figures.Add(pathFigure); 

pathFigure.Segments.Add(curve); 
System.Windows.Shapes.Path p = new Path(); 
p.Data = path; 
this.mainWindow.MyCanvas.Children.Add(path); 

任何幫助將不勝感激!

回答

5

您必須添加pPath)到Canvas,不pathPathGeometry)。

BezierSegment curve = new BezierSegment(new Point(11,11), new Point(22,22), new Point(15,15), false);   

// Set up the Path to insert the segments 
PathGeometry path = new PathGeometry(); 

PathFigure pathFigure = new PathFigure(); 
pathFigure.StartPoint = new Point(11, 11); 
pathFigure.IsClosed = true; 
path.Figures.Add(pathFigure); 

pathFigure.Segments.Add(curve); 
System.Windows.Shapes.Path p = new Path(); 
p.Stroke = Brushes.Red; 
p.Data = path; 

MyCanvas.Children.Add(p); // Here 
+1

+1,打我,並很好的佈局= d – Chris

+1

@克里斯:加上你嘗試:)。 –

+0

D'oh,謝謝你們兩位! – Ace

2

我不是附近的一臺機器,現在可以檢查它,但我認爲你快到了。你需要用一些參數添加您創建的System.Windows.Shapes.Path對象(您目前還沒有使用它),沿使行實際上渲染:

System.Windows.Shapes.Path p = new Path(); 
p.Data = path; 
p.Fill = System.Windows.Media.Brushes.Green; 
p.Stroke = System.Windows.Media.Brushes.Blue; 
p.StrokeThickness = 1; 

this.MyCanvas.Children.Add(p);