Keyvan Nayyeri

God breathing through me

Host Windows Forms Controls in WPF

Cool!  I'm still here and have enough time to waste with blogging!

In one of my recent posts about a free GridView control for Windows Presentation Foundation I stated that previously I was hosting a traditional Windows Forms GridView in my WPF applications to have a GridView.  This is a helpful feature since there are some differences in WPF and Windows Forms controls and the ability to host Windows Forms controls in Windows Presentation Foundation can solve some problems in short time.

Process is simple.  First you need to add a HWND to your WPF applications in UI to host Windows Form controls there.  Actually you must derive a class from HwndHost base class to be able to do this but thankfully Microsoft has done the main job and has added a default implementation that is suitable for many common needs and it's WindowsFormsHost.

To use this class you need to add two references to System.Windows.Forms and WindowsFormsIntegration assemblies.  As you know first one deals with Windows Form controls but second one is an assembly that lets you to add HWND to WPF applications and enables interoperability for WPF with Windows Forms.  So you need to add two new namespaces to your WPF application or XAML file to add HWND to them.

<Window x:Class="HostWinFormInWPF.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"

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

    Title="Host WinForm Controls in WPF" Height="200" Width="300"

    >

Now that you have added these namespaces to application, it's possible to use WindowsFormsHost from Windows Forms Integration to add HWND to your application.  This element provides a hosting surface for your Windows Forms controls.  you can use its Width and Height properties to set your appropriate size for hosting surface.

<Window x:Class="HostWinFormInWPF.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"

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

    Title="Host WinForm Controls in WPF" Height="200" Width="300"

    >

  <Grid>

    <wfi:WindowsFormsHost Width="250" Height="150" />

  </Grid>

</Window>

WindowsFormsHost can contain controls from Windows Forms  as its children.  For example I add a DataGridView to my application and set a name for it.  Later I'll use this name to refer to this control and get access to its properties and methods.

<Window x:Class="HostWinFormInWPF.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"

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

    Title="Host WinForm Controls in WPF" Height="200" Width="300"

    >

  <Grid>

    <wfi:WindowsFormsHost Width="250" Height="150">

      <wf:DataGridView x:Name="gridView" />

    </wfi:WindowsFormsHost>

  </Grid>

</Window>

Now I can go to code behind and write my logic to test this hosted GridView.  First I create a new class for Person for my testing purposes.

using System;

using System.Collections.Generic;

using System.Text;

 

namespace HostWinFormInWPF

{

    internal class Person

    {

        public Person(string name, int age)

        {

            this.name = name;

            this.age = age;

        }

 

        private string name;

 

        public string Name

        {

            get { return name; }

            set { name = value; }

        }

 

        private int age;

 

        public int Age

        {

            get { return age; }

            set { age = value; }

        }

    }

}

I create a list of Persons on fly and will use it as data source for my hosted GridView to implement a binding and test my control.

public Window1()

{

    InitializeComponent();

 

    DataGridView grid = this.FindName("gridView") as DataGridView;

 

    List<Person> list = new List<Person>();

    list.Add(new Person("Keyvan Nayyeri", 22));

    list.Add(new Person("Hamed Banaei", 25));

 

    grid.DataSource = list;

    grid.Refresh();

}

Running this application I get my expected result:

+ Sweet!  After a long time today I could sleep like a human!

18 Comments

DotNetKicks.com
Feb 20, 2007 10:19 AM
#
You've been kicked (a good thing) - Trackback from DotNetKicks.com

KNR
Mar 06, 2007 11:45 PM
#
I tried this in VB.net but I am getting the error while i am using the datagridview. Value of type 'System.Windows.Forms.DataGridView' cannot be converted to 'System.Windows.UIElement'. Can you help me?

Keyvan Nayyeri
Mar 10, 2007 12:10 AM
#
Previously I discussed about Hosting Windows Forms Controls in Windows Presentation Foundation . This

Bindu
May 30, 2007 3:29 AM
#
Hi, Have a doubt, Can this can be placed anywhere in screeen,I am trying a lot it is placed at center. Can Anyone please help me.... Thaks,

Bindu
Jun 17, 2007 4:33 AM
#
Hi, I unable to validate Controls in WPF..... can u pls help me.... Regards, Bindu...

crisha
Jul 18, 2007 10:15 PM
#
Hi keyvan Thanks for your valuable information. This is a very nice article on hosting form control in WPF. My issue is similar to the topic. I need to host infopath form in WPF windows. I used to do using Microsoft.Office.Infopath.FormControl while hosting in windows application. I tried with the following code in XAML but it didnot work. Please let me know if I am including the namespace in a right way or any other code to include along with it. The error is as follows.. The tag 'FormControl' does not exist in XML namespace 'clr-namespace:Microsoft.Office.Infopath.FormControl;assembly=Microsoft.Office.Infopath'. Thanks in advance... crisha.

Habib Sabet
Nov 26, 2007 8:54 PM
#
Hi Keyvan, Very useful information. I implemented it and got results. I did it with a DateTimePicker. Thank you for this and all the best Habib

Khamza Davletov
Apr 01, 2008 9:04 AM
#

Hi, thanks for the blog.

Do you know how to implement WinForms like ToolStrip, with those cool ToolStripDropDown, ToolStripXXXs? Or dou think it's right way to use WindowsFormsHost?


mehrdad
Jul 09, 2008 12:12 AM
#

hi, how we can set color of a pixel in wpf?


RonRap
Sep 11, 2008 8:26 PM
#

what's the meaning of "HWND"¿¿?


lnels
Nov 03, 2008 9:07 AM
#

Superb! Works like a charm...


Matthew
Feb 04, 2009 7:25 AM
#

Excellent and simple,

Was just what I needed. Very useful


SSN
Nov 20, 2009 12:13 AM
#
I used WindowsFormsHost to contain the DataGridView on WinXP64,but when I am to close my APP, my machine has been shut down.Do you have met such problem?
Pingback from WPF Related Links « QuantuMatrix’s Weblog

Turóczy Attila szakmai blogja
Jan 02, 2010 9:19 AM
#
Bár a WPF rengeteg kontrolt bocsájt a rendelkezésünkre, mégis vannak olyanok, amelyek Windows Forms világban
Pingback from http://msportal.hu/blogs/turczy_attila/archive/2010/01/02/a-windows-forms-233-s-a-wpf.aspx
Pingback from http://devportal.hu/blogs/turoczyattila/archive/2010/01/02/a-windows-forms-233-s-a-wpf.aspx
Pingback from http://devportal.hu/blogs/turoczyattila/archive/2010/01/02/a-windows-forms-233-s-a-wpf.aspx

Leave a Comment





Ads Powered by Lake Quincy Media Network