Monday, August 2, 2010

WPF - Hosting Window Form Controls in WPF

Step 1:


Create a New Project and select WPF Application Template and give the name as WinControl_WPF.


Step 2:


Go to the Solution Explorer, Right Click on references menu item and then click Add Reference context menu item.


Step 3:


Select .Net Tab and add Two Assembly.


1. WindowsFormsIntegration
2. System.Windows.Forms


Step 4:


Go to the ControlBox, in Control section select the WindowsFormsHost Control and drag and drop control in WPF Window or you can add these lines in XAML Code into XAML editor.




   1:  <WindowsFormsHost Height="100" Name="WindowTextBox" VerticalAlignment="Top" >
   2:             
   3:          </WindowsFormsHost>


Step 5:



In Window tag add this following attribute.

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

it is look like this:

   1:  <Window x:Class="WinControl_WPF.Window1"
   2:      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:      xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
   5:      Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
   6:      <Grid>
   7:   
   8:          <WindowsFormsHost Height="100" Name="WindowTextBox" VerticalAlignment="Top" >
   9:             
  10:          </WindowsFormsHost>
  11:          
  12:   
  13:      </Grid>
  14:  </Window>

Step 6:



In windowFormsHost Tag add a window controls,it’s look like this:

   1:  <WindowsFormsHost Height="23" Name="WindowTextBox" VerticalAlignment="Top">
   2:              <wf:TextBox x:Name="txtTitle"></wf:TextBox>
   3:  </WindowsFormsHost>

you can add another control like example Button

   1:  <WindowsFormsHost Name="WindowButton" Height="30">
   2:              <wf:Button Name="btnOk" Text="OK"></wf:Button>
   3:  </WindowsFormsHost>

Finally it’s look like this:



XAML CODE:

   1:  <Window x:Class="WinControl_WPF.Window1"
   2:      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:      xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
   5:      Title="Window1" Height="300" Width="300">
   6:      <Grid>
   7:   
   8:  <WindowsFormsHost Height="23" Name="WindowTextBox" VerticalAlignment="Top">
   9:              <wf:TextBox x:Name="txtTitle"></wf:TextBox>
  10:          </WindowsFormsHost>
  11:          
  12:  <WindowsFormsHost Name="WindowButton" Height="30">
  13:              <wf:Button x:Name="btnOk" Text="OK" Click="Button_Click"></wf:Button>
  14:          </WindowsFormsHost>
  15:   
  16:      </Grid>
  17:  </Window>

Step 7:



If you want add button Click event add the following attribute.


find the click event and select the <New Event Handler> enum,it will be automatically create event in code behind.

   1:  <WindowsFormsHost Name="WindowButton" Height="30">
   2:              <wf:Button x:Name="btnOk" Text="OK" Click="Button_Click"></wf:Button>
   3:          </WindowsFormsHost>

Go to the Code behind add the code in button click event

   1:          private void Button_Click(object sender, EventArgs e)
   2:          {
   3:              try
   4:              {
   5:                  System.Windows.MessageBox.Show(txtTitle.Text.Trim());
   6:              }
   7:              catch (Exception ex)
   8:              {
   9:                  System.Windows.MessageBox.Show(ex.Message);
  10:              }
  11:          }


Download
Download Source Code

No comments:

Post a Comment