如何:使用 Polygon 項目繪製封閉的形狀
此範例會示範如何使用 Polygon 元素繪製封閉圖形。 若要繪製封閉圖形,請建立 Polygon 元素,並使用其 Points 屬性來指定圖形的頂點。 會自動繪製連接第一個和最後一個點的線條。 最後,指定 Fill、Stroke 或兩者。
範例
在 Extensible Application Markup Language (XAML) 中,點的有效語法是以空格分隔的 x 和 y 坐標組清單 (以逗號分隔)。
<Canvas Height="300" Width="300">
<!-- Draws a triangle with a blue interior. -->
<Polygon Points="10,110 60,10 110,110"
Fill="Blue" />
<!-- Draws a triangle with a blue interior and a black outline.
The Canvas.Top setting moves the Polygon down 150 pixels. -->
<Polygon Points="10,110 60,10 110,110"
Fill="Blue"
Stroke="Black" StrokeThickness="4"
Canvas.Top="150" />
<!-- Draws another triangle with a blue interior.
The Canvas.Left setting moves the Polygon 150 pixels to the right. -->
<Polygon Points="10,110 110,110 110,10"
Fill="Blue"
Canvas.Left="150" />
<!-- Draws a triangle with a black outline.
The Canvas.Left and Canvas.Top settings move
the Polygon down 150 pixels and 150 pixels to the right.-->
<Polygon Points="10,110 110,110 110,10"
Stroke="Black" StrokeThickness="4"
Canvas.Left="150" Canvas.Top="150" />
</Canvas>
雖然該範例使用 Canvas 來包含多邊形,但您可以使用多邊形元素 (以及所有其他圖形元素) 來搭配任何支援非文字內容的 Panel 或 Control。
此範例是較大範例的一部分;如需完整的範例,請參閱圖形元素範例 (英文)。