blogs.conchango.com

welcome to the conchango blogging site
Welcome to blogs.conchango.com Sign in | Join | Help
in Search

Jomit's Blog

Technical Enthusiast

Using Windows Forms Controls in WPF Applications

We all know that WPF provides a very rich environment for creating applications. But even though sometimes we may want to use windows forms controls to get some specific functionality. For Example you may want to use the DataGrid control or MaskEdit textbox in your WPF application, which is not currently in the control library of WPF.

 

WindowsFormsIntegration library helps us achieve this in a very easy way. Let’s see how:

There are 2 important classes in this library
ElementHost and WindowsFormsHost.

For instance, if you want to use the DataGrid control in your existing WPF application the first you need to do is add the reference to these assemblies:

·         Windows Forms assembly.

·         WindowsFormsIntegration assembly. (The default location for this file is %programfiles%\Referencessemblies\Microsoft\Framework\v3.0\WindowsFormsIntegration.dll )

 

And then write the following code to instantiate the control:

XAML

<Window x:Class="WPFIntegration.Window1"
     xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
     xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
     Title="WPFIntegration" Height="300" Width="300">

   
<
Grid Name="grid1">

Window x:Class="WPFIntegration.Window1"
     xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
     xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
     Title="WPFIntegration" Height="300" Width="300">

   
<
Grid Name="grid1">

       

    </Grid>

</Window>

 

C# Code

System.Windows.Forms.Integration.WindowsFormsHost frmHost = new System.Windows.Forms.Integration.WindowsFormsHost();

DataGrid myGrid = new DataGrid();

frmHost.Child = myGrid;
this.grid1.Children.Add(frmHost);

 

Similarly we can also use WPF controls in Windows Forms using ElementHost. You can find more details Here.

 

There are few Layout Considerations for the WindowsFormsHost Element but still I think it provides a good integration between both control libraries.

 

 

-Jomit

Published 28 February 2007 09:35 by Jomit.Vaghela

Comments

No Comments
Anonymous comments are disabled
Powered by Community Server (Personal Edition), by Telligent Systems