2011-12-10 19 views
0

私はwpfでパスを使用する際に新しいですし、xamlコードのセグメントをC#コードに変換する方法がわかりません。誰かが私にこれを手伝ってもらえますか?私はxamlコードに続いてそれを変換しようとしています。 C#コードには何が欠けていますか?私が尋ねたいもう一つのことは、グリッドが十分なので、ウィンドウに表示されるパスであるかどうかです。 xamlをC#コードに変換する

<Path Stroke="Black" StrokeThickness="1"> 
<Path.Data> 
<PathGeometry> 
    <PathGeometry.Figures> 
    <PathFigureCollection> 
     <PathFigure StartPoint="10,100"> 
     <PathFigure.Segments> 
      <PathSegmentCollection> 
      <QuadraticBezierSegment Point1="200,200" Point2="300,100" /> 
      </PathSegmentCollection> 
     </PathFigure.Segments> 
     </PathFigure> 
    </PathFigureCollection> 
    </PathGeometry.Figures> 
</PathGeometry> 

私のC#コード:

Path myPath = new Path(); 
myPath.Stroke = Brushes.Black; 
myPath.StrokeThickness = 1 
PathGeometry myPathGeometry = new PathGeometry(); 
myPathGeometry.Figures = new PathFigureCollection(); 

PathFigure myPathFigure = new PathFigure(); 
myPathFigure.StartPoint = new Point(10, 100); 
myPathFigure.Segments = new PathSegmentCollection(); 
QuadraticBezierSegment theSegment = new QuadraticBezierSegment(); 
theSegment.Point1 = new Point(200, 200); 
theSegment.Point2 = new Point(100, 300); 
myPathFigure.Segments.Add(theSegment); 
myPathGeometry.Figures.Add(myPathFigure); 

答えて

1

あなたは、最後に

myPath.Data = myPathGeometry; 

を次の行を追加する必要がありますそして、あなたは、xを追加する必要があります<Grid x:Name='myGrid'>としてあなた<Grid>に名前を

と形状がウィンドウに表示されません。しかし、

myGrid.Children.Add(myPath); 
+0

ありがとうございます。私はすでにデータを追加しましたが、私はmyGrid.Children.Add(myPath)を見逃していました。 – arjacsoh

0

あなたのC#コードは、WPFマークアップのような多くを見ることができます。 。ちょうどあなたがそれを表示したいコントロールへのパスを追加

var myPath = new Path 
{ 
    Stroke = Brushes.Black, 
    StrokeThickness = 1.0, 
    Data = new PathGeometry 
    { 
     Figures = new PathFigureCollection 
     { 
      new PathFigure 
      { 
       StartPoint = new Point(10, 100), 
       Segments = new PathSegmentCollection 
       { 
        new QuadraticBezierSegment 
        { 
         Point1 = new Point(200, 200), 
         Point2 = new Point(300, 100), 
        }, 
       }, 
      }, 
     }, 
    }, 
}; 
myGrid.Children.Add(myPath); 
+0

を1行を追加します。 xamlファイルにはしかありません。どうしましたか?出現するパスをどこに追加する必要がありますか? – arjacsoh

+0

グリッドに名前を付けて、パスを追加するだけです。 –

関連する問題