Adding Windows Form Application In WPF

Datetime:2016-08-23 03:56:32          Topic: WPF           Share

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:

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,

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


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,

Code for Mainpage.xaml.cs

private void close_Click(object sender, RoutedEventArgs e) {  
    this.Close();  
}  
Code explanation:

This code will help you to close the Window. 

Step 4:Click Start to run the code.

Clicking Close will close the Window.





About List