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#.
- To create a simple wpf application, open VS 2010 and click on menu File -> New -> Project…
WPF Application – Create new Project
- Select WPF Application, and specify the project name. In this example the name of the Project is kept as “FirstWPFApplication“.
Create a new WPF Application
Select the location of the project and Click on “OK” button to create a simple wpf application project.
- 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
- Open a Solution Explorer, and you will find VS 2010 has added some auto generated files to our solution –
WPF Application – Auto generated files
- Now add button and textbox controls to your application. To add button and textbox, open Toolbox and expand Common WPF Controls.
WPF Application – Toolbox
- 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
- 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
- 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 – Selecting Button_Click event
- 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 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…!!!”
- 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
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 …
- Introduction to WPF.
- How to handle events in WPF.
- Understanding Event handling in WPF.
- How to consume WCF service in WPF application.
- More WPF articles…