WPF Graphics: Drawing a polygon using code behind

This entry is part 8 of 8 in the series WPF Graphics

Drawing a polygon using code behind,

MainWindow.xaml.cs:

using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WpfApplication14
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Polygon polygon;

        public MainWindow()
        {
            InitializeComponent();

            polygon = new Polygon();
            polygon.Stroke = System.Windows.Media.Brushes.Black;
            polygon.Fill = System.Windows.Media.Brushes.LightSeaGreen;
            polygon.StrokeThickness = 2;
            polygon.HorizontalAlignment = HorizontalAlignment.Left;
            polygon.VerticalAlignment = VerticalAlignment.Center;
            System.Windows.Point Point1 = new System.Windows.Point(60, 50);
            System.Windows.Point Point2 = new System.Windows.Point(80, 180);
            System.Windows.Point Point3 = new System.Windows.Point(180, 280);
            System.Windows.Point Point4 = new System.Windows.Point(450, 250);
            System.Windows.Point Point5 = new System.Windows.Point(350, 150);
            PointCollection pointCollection = new PointCollection();
            pointCollection.Add(Point1);
            pointCollection.Add(Point2);
            pointCollection.Add(Point3);
            pointCollection.Add(Point4);
            pointCollection.Add(Point5);

            polygon.Points = pointCollection;
            grid.Children.Add(polygon);
        }
    }
}

MainWindow.xaml:

<Window x:Class="WpfApplication14.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication14"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="grid">
        
    </Grid>
</Window>

polygon

Series Navigation<< WPF Graphics: Drawing a square using code behind