C# WPF First Program


Just to get started, the screen shot of our first program is shown here. Using Visual Studio 2015, we created a new WPF program using C#. Then a button was dragged on to the Designer window. The dimensions of the main window was changed. The Title of the main window was changed. The name of the button was changed from button to mybutton. A click event was added to the button by double-clicking the button in the designer. Message box code was added to the MainWindow.xaml.cs file.

<Window x:Class="WPF01.MainWindow"
        Title="My First WPF Program" Height="200" Width="300">
    <Grid Margin="0,0,0,0">
        <Button x:Name="mybutton" Content="Button" HorizontalAlignment="Left" Margin="40,20,0,0" VerticalAlignment="Top" Width="75" Click="mybutton_Click"/>

Here is the code behind.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WPF01
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()

        private void mybutton_Click(object sender, RoutedEventArgs e)
        {  // double click the button on the Designer to get this
            MessageBox.Show("You clicked me!");

Leave a comment

Your email address will not be published. Required fields are marked *