What is WPF and why we need to choose for desktop application platform

What is WPF (Windows Presentation Foundation)?

WPF is UI framework to implement responsive desktop apps& it build to take advantage of graphics hardware. WPF provides broad setof development features which includes extensible application markup language,data binding, controls, graphics, styles, animation, templates.

Implementation

Currently there are two way of implementing WPF app,

.NET version

This is open-sourceimplementation of WPF application, mainly depend on .NET 5 (+ .Net core 3.1).To implement using .NET version, it requires minimum Visual Studio 2019

.Net Framework

.Net framework implementationrequires minimum of Visual Studio 2008 (In this blog we will discussimplementation using .NET framework 4).

.NET Core is a cross-platformtechnology, although WPF isn't and only runs on Windows.

Markup & code-behindWPF allows you use both markup as well as code-behindapplication development.

Markup


WPF provide XAML to design user interface. You can use XAML to design windows,pages, dialog boxes, user controls. 

 

Here we have created one window and added one button to it.We can create other components as well like User Control, Page etc.
Below is the result of the window we have created.

Code-behind


Code-behind is part of development where we handle all the interactions withour app like click event, radio box selection, toolbar and menu item selection, etc.
Here we have defined click event on the button, which basically points to the methodin code-behind being called when user clicks on the button.



code-behind file will look something like this.


Below is result for code we have added on click event.

Controls

WPF provides several pre-build controls to design &develop interactive user applications. Few of them are as listed below.

Layout

While creating a user interface we must need to arrangecontrols and display data in a specific format for better accessibility, for this, we can use inbuilt layout controls.
Grid, StackPanel, Canvas, WrapPanel, DockPanel, etc are laid out controlsprovided by WPF.


Here is an example of DockPanel usage.


Output:


Data Bindings


There are many cases where we need to show data to user through our applicationinterface.
Consider we are accessing users profile data from SQL db and we need to showthis profile information on UI.
We have created Person class with single property called Name & set the default value to it.



In our XAML we have define binding to the Name property. 


On the code-behind end, we have created a new object of person class & added it asdata context to our current window. XAML uses data binding property used tobind data with our UI component. 



Recommendation: What’s New in .NET5 Framework

Why use WPF?


Theme & Styles

User experience with the interface is an essential part of all modern apps, which we can easily define using simple design tags for components.In traditional WinForm apps changing styles and theme is tedious task &require lots of rework. WPF allows developers to complete an entire applicationfirst without any style & theme applied and designer can design themes,layouts separately later on.
               SinceWPF allows adding theme as an external resource we don’t need to re-compile ourapp when adding/modifying theme changes.

Separation of UI & Business Logic

WPF is designed to create loosely coupled app, where UIcomponents and business logic can be created separately without depending oneach other. You can use Model View View Model (MVVM) architecture in WPF whichmakes separation of UI & business logic much simpler.

Graphics & Animation

WPF allows developer to add custom animation code incode-behind as well as in XAMAL itself, which makes more convenient thantraditional WinForm app.

Community & support

WPF has large community of individual developers as well asthird party vendors designing ready to use custom controls. You can find lotsof sample apps & demo projects for WPF and third party vendors alsoregularly updates their toolkit to support latest WPF development trend.


Creating your first app in WPF.

Step 1

Open Visual Studio & Select “Create New Project”


Step 2

Now you can either select WPF application for .NET coreversion or WPF App for .NET framework.
After selecting any one of these option click Next.


Step 3

On next screen you can define name of project, location ofproject on drive & development framework.
Once you choose all these simply click “create”.


Step 4

Visual studiodefault creates & opens this two files when creating new project.

  • XAML File (MainWindow.xaml)
  • CS File (MainWindow.xaml.cs)

XAML file has view split in two : Design window &XAML Tags.


Design window presents view of your current UI, You can choose to add controlshere by drag-drop from the toolbox OR you can use XAML Tags to add controls towindow.

MainWindow.xaml.cs contains thecorresponding code behind the XAML design file.


Step 5

Drag ‘Label’ element from  tool box to design winodow. You can see XAMLtag will be added automatically when you drop any control within design winodw.