C# // WPF

C#.Net How To: Create a simple WPF Application in Visual Studio 2010

This wpf tutorial explains how to create a simple WPF application in Visual Studio 2010. Before we start, i would suggest to read my previous articles where i have given an introduction to WPF and explained WPF Architecture. It’s not mandatory to read these articles, however these articles would help you to understand what is wpf in .net?

Steps to create a simple WPF application using C#.Net

The goal here is to create a new Windows Presentation Foundation (WPF) application that would show a message in a textbox on button click event. This article would help to understand WPF with C#.

  1. To create a simple wpf application, open VS 2010 and click on menu File -> New -> Project…
    Create a simple WPF Application
    WPF Application – Create new Project
  2. Select WPF Application, and specify the project name. In this example the name of the Project is kept as “FirstWPFApplication“.
    Create a simple wpf application
    Create a new WPF Application

    Select the location of the project and Click on “OK” button to create a simple wpf application project.

  3. As soon as you click on OK button, MainWindow.xaml in Design mode would appear in Visual Studio IDE as shown in below screenshot –
    WPF Application - MainWindow.xaml in Design mode
    WPF Application – MainWindow.xaml in Design mode
  4. Open a Solution Explorer, and you will find VS 2010 has added some auto generated files to our solution –
    WPF Application - Auto generated files
    WPF Application – Auto generated files
  5. Now add button and textbox controls to your application. To add button and textbox, open Toolbox and expand Common WPF Controls.
    WPF Application - Toolbox
    WPF Application – Toolbox
  6. Add textbox control in MainWindows.xaml. You can change the properties of this textbox control the same way as you do in Windows Forms Application.
    WPF Application - Adding Textbox control
    WPF Application – Adding Textbox control
  7. Add a button. Change the button properties from Properties window if required. For this article, i have changed the name of the button to “Click Me..!!” as shown in below image –
    WPF Application - Adding Button control
    WPF Application – Adding Button control
  8. Now, click on “Events” tab in Properties window to add some events to button. In this wpf with c#example we are going to implement “Button_Click” event, hence write “Button_Click” in Click event and press enter.
    WPF Application - Button_Click event
    WPF Application – Selecting Button_Click event
  9. As soon as you press enter, VS 2010 IDE would add auto generate code for Button_Clieck event in MainWindows.xaml.cs file as shown in below screenshot.
    WPF Application - Button_Click event
    WPF Application – Button_Click event code in C#

    Write your code in this event. For this article i have written a code to display following message in textbox on button click –
    “My first WPF Appliation…!!!”

  10. Save all the changes to create a simple wpf application. Your first WPF application is ready now. Press F5 button to execute the application. MainWindow with a textbox and “Click Me..” button would appear. Click on button to get following output.
    Created Simple WPF Application
    Created Simple WPF Application

In this article we learned to use WPF in c#.net. I hope this article helped you to get started with WPF applications.

Sharing is caring. Please share the article on your favourite social media if you think it’s worth to share.

More WPF Related articles …

About the Author: Ashish Ramteke

Ashish Ramteke is a Software Professional, Part time blogger and founder of C# Tutorials, a popular blog on C#.Net. Ashish is having 6 years of work experience and an engineering degree in Computer Science and Engg.

6 Comments+ Add Comment

  • beautiful article……simple to understand

  • HOW TO SET TIMER IN WPF?????????????????

    • Hi Sandra,

      I am very sorry for the late reply. Please see below code –

      DispatcherTimer timer;
      TimeSpan actualTime = new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);

      // Start the Timer.
      timer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
      // Your business logic here.
      actualTime = actualTime.Add(TimeSpan.FromSeconds(+1));
      }, Application.Current.Dispatcher);


  • i m waiting!!!!!!!!!!

    • It is very easy to do suppose you have to make event1 to occur when you click a button and after 1 second event2 to occur.firstly add
      using.windows.threading in heading , now drag a button from toolbox to design area now go to property windows and click on event tab now double click on click in dropdown list the .xaml.cs will open you will see that some syntax as follows:

      now add some codes as mentioned below

      that’s all press f5 to run click on button you will see event1 to occur and after 1sec event 2 will occur……i have used visual studio 2010 to program,thanx.

  • beautiful and very easy to understand article to begin learning WPF. can you explain how we make exe file of the same

Leave a comment