Click here to Skip to main content
15,867,704 members
Articles / Desktop Programming / WPF
Tip/Trick

Host Windows Form Controls in WPF

Rate me:
Please Sign up or sign in to vote.
3.89/5 (9 votes)
24 Nov 2010CPOL 55.5K   8   2
Include Windows Form Controls in WPF

Windows Presentation foundation or WPF provides a rich set of GUI components that are hardware accelerated. Still there comes a time when you are required to put a Windows Form Control into your WPF application. Of course you do not want to re-program your user control just because the technology is changing. By not re-programming it means that you are saving your time and investments you have done on creating a Windows Form control.


I myself faced this problem when I was instructed to include a Webcam control which was purely a Windows form user control. Any ways here's the code:


C#
private void LoadWFUserControl() 
{
    // Initialize a Host Control which allows hosting a windows form control on WPF. Ensure that the WindowsFormIntegration Reference is present.
    System.Windows.Forms.Integration.WindowsFormsHost host =
        new System.Windows.Forms.Integration.WindowsFormsHost();
    // Create an object of your User control.
    MyWebcam uc_webcam = new MyWebcam();
    // Assign MyWebcam control as the host control's child.
    host.Child = uc_webcam;
    // Add the interop host control to the Grid
    // control's collection of child controls.
    this.grid1.Children.Add(host);
}

VICE-VERSA


The other way round is also possible i.e. HOSTING A WPF CONTROL IN WINDOWS FORMS APPLICATION.


Follow these steps:



  1. Make a WPF user control DLL (say for example MyControl.dll)
    • Add reference of this DLL to your Windows Form Application.

    • Add reference of following to your project (required for WPF)


    • PresentationCore
    • PresentationFramework
    • System.Xaml
    • WindowsBase
    • WindowsFormsIntegration


Example:


C#
void LoadAPFControl()
{
    //Create and configure your WPF control instance
    System.Windows.Controls.TextBox wpfTextBox =
            new System.Windows.Controls.TextBox();
        wpfTextBox.Name = "txName";
        wpfTextBox.Text = "WPF TextBox";
        wpfTextBox.TextChanged +=
            new TextChangedEventHandler(textbox_TextChanged);
    //Create element host control so as to use WPF control on Windows Form
    ElementHost elementHost = new ElementHost();
    elementHost.Dock = DockStyle.None;
    elementHost.Width = 150;
    elementHost.Height = 50;
    // Assign the control as the host control's child.
    elementHost.Child = wpfTextBox;
 
    // Finally, Add the interop host control to the panel container control's collection of child controls.
    containerPanel.Controls.Add(elementHost);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
http://gputoaster.wordpress.com/about

Comments and Discussions

 
GeneralYes you are right. Ideally you should perform it while desig... Pin
GPUToaster™25-Nov-10 3:05
GPUToaster™25-Nov-10 3:05 
Yes you are right. Ideally you should perform it while designing. As I'm not a master of WPF, but when you want to dynamically load the components, then this method is prefferable. But as you said XAML should be used, AFAIK, using XAML it wont be possible. But It would be great if you could suggest a better (only for dynamic loading) method.
Thanks!
GeneralReason for my vote of 2 Why do this in code? This should be ... Pin
SledgeHammer0124-Nov-10 13:21
SledgeHammer0124-Nov-10 13:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.