Stats

306 Contributors: 7 Sunday, September 11, 2016
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Getting started with wpf

Remarks

WPF (Windows Presentation Foundation) is Microsoft's recommended presentation technology for classic Windows desktop applications. WPF should not be confused with UWP (Universal Windows Platform) although similarities exist between the two.

WPF encourages data driven applications with a strong focus on multimedia, animation and data binding. Interfaces are created using a language called XAML (eXtensible Application Markup Language), a derivative of XML. XAML helps WPF programmers maintain separation of visual design and interface logic.

Unlike its predecessor Windows Forms, WPF uses a box model to layout all elements of the interface. Each element has a Height, Width and Margins and is arranged on screen relative to it's parent.

WPF stands for Windows Presentation Foundation and is also known under its Codename Avalon. It's a graphical Framework and part of Microsofts .NET Framework. WPF is pre-installed in Windows Vista, 7, 8 and 10 and can be installed on Windows XP and Server 2003.

Versions

Version 4.6.1 - December 2015

Hello World application

To create and run new WPF project in Visual Studio:

  1. Click File → New → Project

New project

  1. Select template by clicking Templates → Visual C# → Windows → WPF Application and press OK:

enter image description here

  1. Open MainWindow.xaml file in Solution Explorer (if you don't see Solution Explorer window, open it by clicking View → Solution Explorer):

enter image description here

  1. In the XAML section (by default below Design section) add this code
<TextBlock>Hello world!</TextBlock>

inside Grid tag:

enter image description here

Code should look like:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock>Hello world!</TextBlock>
    </Grid>
</Window>
  1. Run the application by pressing F5 or clicking menu Debug → Start Debugging. It should look like:

enter image description here