Step 1 : Design MainWindow.xaml like below


wpf32


Step 2 : Mainwindow.xaml.cs code


Note : For grid tag assign the name as “g1” in xaml mode.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LineDraw
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Line l1 = new Line();
            l1.X1 = 20;
            l1.Y1 = 30;
            l1.X2 = 200;
            l1.Y2 = 30;
            l1.Stroke = System.Windows.Media.Brushes.Red;
            l1.StrokeThickness = 5;
            g1.Children.Add(l1);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Ellipse e1 = new Ellipse();
            e1.Height = 250;
            e1.Width = 250;
            e1.Stroke = System.Windows.Media.Brushes.Red;
            e1.StrokeThickness = 5;
            e1.Fill = System.Windows.Media.Brushes.Yellow;
            g1.Children.Add(e1);
        }
    }
}


Output :


wpf33