Do you know the difference between WPF and WinForms?

Do you know the difference between WPF and WinForms?

Introduces the main differences between two methods of developing Windows desktop applications, which can play a better role in modern system development.

Last updated 4/7/2021 11:59 PM
沙漠尽头的狼
7 min read
Category
WPF Winform
Tags
.NET C# WPF Winform

Introduction

The abbreviation WPF refers to Microsoft’s Windows Presentation Foundation, while WinForms is simply short for Windows Forms Applications. Both are Microsoft’s Windows application graphical user interfaces that developers can use to build Windows desktop applications. This article focuses on the key differences between these two approaches to developing Windows desktop applications, which can be better suited for modern system development.

Windows Forms

WinForms was introduced in February 2002 as part of the .NET Framework. For the most part, WinForms allows developers to drag and drop controls onto a Windows form and manipulate those controls using code-behind files that can be written in C#, VB.NET, or any other .NET language. Each WinForms control is an instance of a class, since WinForms exists as a wrapper over a set of C++ classes. Microsoft’s Visual Studio makes WinForms development easier because developers can simply drag and drop controls from the Toolbox.

Controls in the WinForms Toolbox:

Controls in the WinForms Toolbox

In a WinForms desktop application, developers only have access to the code-behind file where they can handle control events. WinForms desktop applications have limitations in terms of control functionality and application behavior, which will be revealed in the next section.

WPF Desktop Applications

Unlike WinForms, WPF’s architecture consists of three main components: a presentation framework, presentation core, and mallcore. WPF does not rely entirely on standard Windows controls, making it a standalone approach. In 2007, Microsoft introduced Windows Presentation Foundation (WPF) as an alternative to WinForms for .NET Framework desktop application development. This alternative brought many changes to desktop application development. First, WPF separates designers and programmers: the UI can be designed separately using Visual Studio or Blend, while developers can use code-behind files to handle control events.

WPF uses XAML to create controls, and its file structure is more like ASP.NET. You are free to use the designer or write XAML code to create controls. Designers using the Canvas Panel can still drag and drop controls onto a Windows page just like in WinForms. The main difference introduced by WPF is the XAML file and access to the visual designer that accompanies the XAML file.

WPF Visual Designer and XAML File Editing:

WPF Visual Designer and XAML File Editing

The image above shows the layout of a WPF application, where the XAML file is displayed alongside the Designer.

The file structure of a WPF project is as follows:

File Structure of a WPF Project

  • Each window or page has a .xaml file for adding controls and a .cs, .vb, etc. code-behind file, similar to the ASP.NET approach.
  • Unlike WinForms, WPF generates an initial MainWindow to start the application. To change the startup window, you can do so in the App.xaml file.

WPF Main Window Startup Configuration:

WPF Main Window Startup Configuration

  • This file acts as the entry point of the application.

Another notable difference between WPF and WinForms is the controls. To add a control, you simply write simple XAML code. For example, to add a text box in a WPF window, you can write code like this:

<Window
  x:Class="WpfApp1.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:WpfApp1"
  mc:Ignorable="d"
  Title="MainWindow"
  Height="450"
  Width="800"
>
  <StackPanel>
    <TextBox></TextBox>
  </StackPanel>
</Window>

Notice the markup in the syntax, which suggests the name “Extensible Application Markup Language (XAML).” XAML code is placed inside the Window tag. Control tags may have attributes describing the control’s width, height, etc., depending on the control.

WPF also brings another significant difference from WinForms: the ability to add a Button with an image. In WinForms, adding an image to a button means you have to draw the image yourself or include some third‑party control, but the WPF Button control is straightforward – you can add anything inside it.

<Window
  x:Class="WpfApp1.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:WpfApp1"
  mc:Ignorable="d"
  Title="MainWindow"
  Height="500"
  Width="800"
>
  <Button Padding="5">
    <StackPanel Orientation="Horizontal">
      <Image Source="/Image.jpg" Height="25" Width="50" />
      <TextBlock Margin="5,0">I'm a Button</TextBlock>
    </StackPanel>
  </Button>
</Window>

The output looks like this: WPF Run Demo

WPF Run Demo

WPF also provides fully supported data binding, as shown in the following example:

<Window
  x:Class="WpfApp1.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:WpfApp1"
  mc:Ignorable="d"
  Title="MainWindow"
  Height="500"
  Width="800"
>
  <StackPanel Margin="10">
    <WrapPanel Margin="0,10">
      <Label Content="Your Text Here:" FontWeight="Bold" />
      <TextBox
        Name="txtBind"
        Height="20"
        Width="250"
        RenderTransformOrigin="-2.75,0.587"
        Margin="59,0,336,0"
      />
    </WrapPanel>
    <WrapPanel Margin="0,10">
      <TextBlock Text="Bound-Text: " FontWeight="Bold" />
      <TextBlock Text="{Binding Path=Text, ElementName=txtBind}" />
    </WrapPanel>
  </StackPanel>
</Window>

Output: WPF Data Binding Demo

WPF Data Binding Demo

The {Binding} attribute in the above example is used to bind the text in the <TextBlock> to the text in the txtBind TextBox. This simply illustrates how easy it is to bind data in WPF using the {Binding} attribute.

Conclusion

This article demonstrates the key differences between WinForms and WPF through architectural, syntactical, file structure, and application behavior differences between the two .NET approaches to creating desktop applications. Although the WinForms designer may appear friendly and straightforward, XAML brings some useful features that developers may need in modern desktop applications.

Original link: https://www.c-sharpcorner.com/article/wpf-vs-winforms/

Keep Exploring

Related Reading

More Articles
Same category / Same tag 4/7/2022

What is the difference between Winform and WPF?

Some friends always ask, 'What is the difference between WinForm and WPF?' At first thought, this question seems simple to answer, but I never systematically analyzed it. Today, I took the time to write an article that only represents my personal views to record a summary.

Continue Reading