Adding Windows Form Application In WPF

Introduction

Here, we will be working with adding Windows Form Application, where we can add name, employee ID and the company details in Windows Presentation Foundation (WPF).

What is Winform?

Windows Form is a segment of Microsoft .NET framework. It is a GUI (Graphical User Interface) class library, which allows us to write rich looking client Applications for tablets, laptops and desktops.

Development Requirements

  1. Visual Studio 2015
  2. .NET Framework 4.6
  3. Microsoft Expression Blend if needed

Follow the following steps to add Winforms in WPF

Step 1: Run Visual Studio 2015 -> Visual C# -> Windows -> WPF Application, as shown below:

WPF Application

Step 2: Add the following namespacing too,

xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

Copy the code, given below, into MainWindow.xaml,

  1. <WindowsFormsHost HorizontalAlignment="Left" Height="321" VerticalAlignment="Top" Width="519" Margin="0,0,-0.333,-0.333" Background="#FF987676" />  
  2. <Label Content="Name" HorizontalAlignment="Left" Margin="72,44,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.009,0.397" />  
  3. <Label Content="Employee ID" HorizontalAlignment="Left" Margin="72,92,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.009,0.397" />  
  4. <Label Content="Company" HorizontalAlignment="Left" Margin="72,147,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.009,0.397" />  
  5. <TextBox HorizontalAlignment="Left" Height="23" Margin="252,44,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="227" />  
  6. <TextBox HorizontalAlignment="Left" Height="23" Margin="252,96,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="227" />  
  7. <TextBox HorizontalAlignment="Left" Height="23" Margin="252,151,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="227" />  
  8. <Button Name="close" Content="Close" HorizontalAlignment="Left" Margin="404,206,0,0" VerticalAlignment="Top" Width="75" Click="close_Click" />  
code

WindowsFormsHost: It is used to add Windows Forms in WPF

Alignment: Vertical alignment and horizontal alignment is used to align the elements that are added

Summary

Here, we have added Windows Form, in which we have added three labels – Name, Employee ID and Company with TextBox of Horizontal Alignment and text wrapping added with a button-close. The close button function is to close the Window. The margins for all the elements are added in this Windows Form with Height, Content, Width, Background color etc.,

Step 3: Add click event handler by double clicking on the Close button in designer pane. It will redirect you for MainWindow.xaml.cs,

xaml

Code for Mainpage.xaml.cs
  1. private void close_Click(object sender, RoutedEventArgs e) {  
  2.     this.Close();  
  3. }  
Code explanation: This code will help you to close the Window. 

Step 4: Click Start to run the code.

code

code

code

Clicking Close will close the Window.

window

window

Up Next
    Ebook Download
    View all
    Learn
    View all