Comment : créer un LineSegment dans un PathGeometry

Cet exemple indique comment créer un segment de ligne. Pour créer un segment de ligne, utilisez les classes PathFigureet LineSegment les PathGeometryclasses.

Exemple

Les exemples suivants dessinent une LineSegment valeur comprise entre (10, 50) et (200, 70). L’illustration suivante montre le résultat LineSegment; un arrière-plan de grille a été ajouté pour afficher le système de coordonnées.

A LineSegment in a PathFigure LineSegment tiré de (10 50) à (200 70)

Dans XAML (Extensible Application Markup Language), vous pouvez utiliser la syntaxe d’attribut pour décrire un chemin d’accès.

<Path Stroke="Black" StrokeThickness="1"
  Data="M 10,50 L 200,70" />

(Notez que cette syntaxe d’attribut crée en fait une StreamGeometryversion plus légère d’un PathGeometry. Pour plus d’informations, consultez la page Syntaxe du balisage de chemin d’accès.)

En XAML, vous pouvez également dessiner un segment de ligne à l’aide de la syntaxe de l’élément objet. Voici l’équivalent de l’exemple XAML précédent.

<Path Stroke="Black" StrokeThickness="1">
  <Path.Data>
    <PathGeometry>
      <PathFigure StartPoint="10,50">
        <LineSegment Point="200,70" />
      </PathFigure>
    </PathGeometry>
  </Path.Data>
</Path>
PathFigure myPathFigure = new PathFigure();
myPathFigure.StartPoint = new Point(10, 50);

LineSegment myLineSegment = new LineSegment();
myLineSegment.Point = new Point(200, 70);

PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
myPathSegmentCollection.Add(myLineSegment);

myPathFigure.Segments = myPathSegmentCollection;

PathFigureCollection myPathFigureCollection = new PathFigureCollection();
myPathFigureCollection.Add(myPathFigure);

PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = myPathFigureCollection;

Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
myPath.Data = myPathGeometry;
Dim myPathFigure As New PathFigure()
myPathFigure.StartPoint = New Point(10, 50)

Dim myLineSegment As New LineSegment()
myLineSegment.Point = New Point(200, 70)

Dim myPathSegmentCollection As New PathSegmentCollection()
myPathSegmentCollection.Add(myLineSegment)

myPathFigure.Segments = myPathSegmentCollection

Dim myPathFigureCollection As New PathFigureCollection()
myPathFigureCollection.Add(myPathFigure)

Dim myPathGeometry As New PathGeometry()
myPathGeometry.Figures = myPathFigureCollection

Dim myPath As New Path()
myPath.Stroke = Brushes.Black
myPath.StrokeThickness = 1
myPath.Data = myPathGeometry

Cet exemple fait partie d’un exemple plus vaste ; pour l’exemple complet, consultez Géométries, exemple.

Voir aussi