Saturday 26 April 2014

Adding WPF Windows to an existing Windows Form Project



Adding WPF Windows to an existing Windows Form Project

Last week I was working on one window application. In this application we have to add WPF window to existing Window form project


Following code describes how to add WPF window to existing Window form project

There are 2 ways we can add WPF window to existing window form application


1. Using Designer  Side


1.      Add reference to System.Windows.Forms and WindowsFormsIntegration.




Select System.Windows.Forms








Once these two references are added, we have access to all windows forms controls and access to the components that will allow us to embed them in WPF.






2. In the WPF Tool Box, we can see WindowsFormsHost control




Drag and Drop the WindowsFormsHost Control in WPF Window.

Our XAML is going to be extremely simple:




<Window x:Class = "Window1" >
xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="278" Width="587"
<Grid>
<WindowsFormsHost Name="WindowsFormsHost1" >

</Grid>
</Window







2. Using Code Behnind file



using System;
using System.Windows.Forms;
using System.Windows.Forms.Integration;

var wpfwindow = new WPFWindow.Window1();

ElementHost.EnableModelessKeyboardInterop(wpfwindow);

wpfwindow.Show();




In the above code The EnableModelessKeyboardInterop() call is necessary to handle keyboard input in the WPF window if loaded from a non-WPF host like WinForms.

Happy Programming!!

If you have any query mail me to Sujeet.bhujbal@gmail.com     


Regards

Sujeet Bhujbal

------------------------------------------------------------------------------------------------------------
---------------

1 comment:

  1. Hi Sir,
    I had tried your application but it is not working.

    ReplyDelete