WPF Introduction

From WikiOD

Remarks[edit | edit source]

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[edit | edit source]

Version 4.6.1 - December 2015

Hello World application[edit | edit source]

To create and run new WPF project in Visual Studio:

  1. Click File → New → Project

New project

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

Getting_started_with_wpf

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

Getting_started_with_wpf

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

inside Grid tag:

Getting_started_with_wpf

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>
  • Run the application by pressing F5 or clicking menu Debug → Start Debugging. It should look like:

Getting_started_with_wpf

Credit:Stack_Overflow_Documentation