C# Windows Presentation Foundation (WPF)

    0 Votes

The WPF is a user interface (UI) framework that creates a rich, interactive client application. The WPF development platform support a set of application development features which includes a resources, layout, graphics, controls, application model, data binding, documents and security.

The WPF is a subset of the .NET Framework, so if there are previously built applications within the .NET Framework using ASP.Net or Windows forms then the programming experience will be familiar. The WPF uses the Extensible Application Markup Language as it provides a declarative model for application programming.

WPF application includes the elements that re common to most of the WPF applications such as XAML, data binding, controls, application definitions, layout, code-behind and styles. The XAML design the appearance of the user interface application.

WPF v/s Win Forms

WPFWin Forms
It is a graphical subsystem that provides user interfaces in Windows based applications. It is a graphical API which provides access to native Microsoft Windows interface elements.
It is a mark up language that defines UI elements and relationships with other UI elements. It is an event driven application that is supported by Microsoft .Net Framework.
It supports window forms. It supports WPF.

 

Creating WPF application

In this we are going to create WPF application. The steps to create WPF application are:

  • Open Visual Studio 2010 and click on menu File-> New-> Project.
  • Select WPF Application and specify the project name. For example we have assigned WPFApplication1. After that select the location of the project and then click OK to create a simple WPF application project.
  • After clicking the OK button the MainWindow.xaml in design mode will appear in Visual Studio IDE.
  • Open the Solution Explorer and then you will find Visual Studio 2010 has added some auto generated files to our solution.
  • Now we will add button and textbox controls to our application. To add textbox and button, open Toolbox and expand Common WPF Controls.
  • Now we will add the textbox control in MainWindows.xaml. We can change the properties of the textbox control.
  • Now add a button and change the properties from the properties window if needed.
  • After that click on Events tab in Properties Window to add some events to button. Like Button_Click event in Click event and then press enter.
  • After pressing enter Visual Studio 2010 IDE will add auto generate code for Button_Click event in MainWindow.xaml.cs file. After that write your own code in this event.
  • Save all the changes made to create a WPF application. Now press F5 button to execute the application. MainWindow with textbox and button will appear.

Let us have an example:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="72">
                        Hello, Welcome to WPF
                </TextBlock>
    </Grid>
</Window>

XAML

The XAML stands for Extensible Application Markup Language. It is a simple language which is based on XML to create and initialize .NET objects with hierarchical relations. It was originally invented for WPF as it is used to create any kind of object trees. Today we can use XAML to create user interfaces in WPF, Silverlight, declare workflows in Windows Forms and for electronic paper in the XPS standard. All the classes in WPF have parameterless constructors and make excessive usage of properties.

Advantages

  • The code is short and clear to read.
  • It provides the separation of designer code and logic.
  • The graphical design tools like Expression Blend require XAML as source.
  • Separation of XAML and UI logic allows us to separate the roles of designer and developer.

WPF Window

The WPF window is of 3 types:

  • Window – This is a normal windowed application where every control is placed within the same window. The client area is fully customizable using XAML.
  • NavigationWindow – This window is a special type of window which is inherited from Windows but with a Navigation panel on top of it. So if we want to create an application that makes sense when used as wizards then we should use NavigationWindow.
  • Page – It is similar to NavigationWindow but the difference between two of them is that page can be opened in Web Browser as XBAP applications.