---
title: "WPF (Windows Presentation Foundation) – Introduction and Sample Code in C# .NET"  
description: "Windows Presentation Foundation (WPF) is Microsoft’s latest technology for creating graphical user interfaces, whether they consist of plain forms, do"  
author: "Amit Singh"  
published: 2010-09-16  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/117/wpf-windows-presentation-foundation-introduction-and-sample-code-in-c-sharp-dot-net  
category: "wpf"  
tags: ["wpf"]  
reading_time: 3 minutes  

---

# WPF (Windows Presentation Foundation) – Introduction and Sample Code in C# .NET

Introduction\

Windows Presentation Foundation (WPF) is Microsoft’s latest technology for creating graphical user interfaces, whether they consist of plain forms, document-centric windows, animated cartoons, videos, immersive 3D environments, or all of the above!

This is a technology that makes it easier than ever to create a broad range of applications.

It removes the limitation of GDI + and User yet provide the kind of productivity that people enjoy with frameworks like window forms.

WPF is a major component of the .NET Framework, starting with version 3.0.

FIGURE: The technologies in the .NET Framework 3.0.

![The technologies in the .NET Framework 3.0](https://www.mindstick.com/mindstickarticle/646a7b2e-46cc-499b-921f-f91ef7576705/images/07f544c0-9b2c-4135-b5bb-a30bd5abf17d.png)

##### Creating UI using WPF

Developing WPF UI is much more like building web UI than native Windows development. How to define UI in WPF

##### Defining WPF UI with XAML

XAML (pronounced *zammel*) is an acronym for e**X**tensible **A**pplication **M**arkup **L**anguage and is an XML-based specification for defining UI Although XAML was created specifically for WPF. This is technically called a *declarative* programming model. WPF will take that definition and convert it into real elements on the screen.

##### For Example:

```
//XAML Code <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><StackPanel><Label>MindStick</Label></StackPanel></Page>
```

XAML documents are *strict* XML. Every tag (for example, StackPanel or Label) and attribute (for example, Margin or FontSize) must correspond to a valid .NET type or property. If a tag has the wrong case or an unknown attribute is used, the resulting XAML won’t work.

##### Defining WPF UI through code

You don’t have to use XAML to define UI elements. You can write code to define your UI, much as you did with Windows Forms. This is the classic imperative programming model.The following code does exactly the same thing as the XAML in above.

##### For Example:

```
//Using CodeWindow window1;Page page1;StackPanel stackPanel1;Label label1; public Procedural(){window1 = new Window();page1 = new Page();stackPanel1 = new StackPanel();label1 = new Label();label1.Content = "Hello, World!";stackPanel1.Children.Add(label1);page1.Content = stackPanel1;window1.Content = page1;}
```

##### Simple application on WPF

**Step1**: we open the visual studio and choose the new project as in figure and we select the WPF application and right above corner we check the version 3.5.

Select the WPF Application

![Select the WPF Application](https://www.mindstick.com/mindstickarticle/646a7b2e-46cc-499b-921f-f91ef7576705/images/eafcee4f-7a89-4537-aaa7-ec6ffe54410b.png)

![WPF Application Form](https://www.mindstick.com/mindstickarticle/646a7b2e-46cc-499b-921f-f91ef7576705/images/8ebb941e-c4f6-483f-b9dd-ae3964be7ca9.png)

**Step 2**: Adding a button control from toolbox and double click on button and write the code

```
      private void button1_Click(object sender, RoutedEventArgs e)         {            MessageBox.Show("This is my first wpf application ");                    }
```

Adding button from toolbox

![Adding button from toolbox (XAML)](https://www.mindstick.com/mindstickarticle/646a7b2e-46cc-499b-921f-f91ef7576705/images/4f517764-66f4-4264-9d1e-c1a25acd8d8d.png)

**Step 3**: Running the application

![Executed WPF Application](https://www.mindstick.com/mindstickarticle/646a7b2e-46cc-499b-921f-f91ef7576705/images/27f6cc25-32af-4e1e-b5bc-558949795037.png)

This is the simple demonstration of WPF application

\

---

Original Source: https://www.mindstick.com/articles/117/wpf-windows-presentation-foundation-introduction-and-sample-code-in-c-sharp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
