How to host WPF Control into the Windows Form Application

Source: Internet
Author: User

Host a WPF control into Win32 Windows Form

 

This article wocould explain how to host a WPF control into the Windows Form. I wocould create the WPF Control, and a Windows form application, which wocould host the WPF control. in the next article we wocould see how event from WPF control cocould be passed to hosting
Windows form.

 

Note:In Windows Form we can host only a WPF element, so we wocould use the Grid element in WPF and put the control (s) into it to make a user control. in this sample I wocould use button and text box WPF controls.

 

Step1. Creating the WPF Control:

  1. Open Visual Studio 2008.
  2. Open the project creation template from File-> Create project menu.
  3. Select the WPF User Control Library.
  4. Name the project as WpfUserControlLib and create the project.
  5. It wocould create UserControl1.xaml and UserControl1.xaml. cs
    In the project.
  6. Inherit the usercontrol1 from the grid elemement instead of the usercontrol, as I stated in the note above we can not host a control rather we can host a element into the windows form. this change has to be done in both
    Usercontrol1.xaml and usercontrol1.xaml. CS.
  7. Now drop the lable, text box and two buttons (OK and cancel ).
  8. Build the wpfusercontrollib.

 

Make sure WpfUserControlLib contains following references, though Visual Studio 2008 wocould do these by itself

    • System
    • Presentationcore
    • Presentationframework
    • Windowsbase

     

     

    Step 2: creating the Windows Forms Host application in the current solution:

     

    1. Open the Project Creation template from file-> Add-> new project menu.
    2. Select windows form application from the Project template and name it
      Winformwpfhostapp
      .
    3. From the Toolbox (WPF interoperability tab) Drop the elementhost control to the form1. it wocould be named as elementhost1.
    4. Set the dock property to fill of elementhost1 control (optional ).
    5. Add the reference of wpfusercontrollib project created in the step1.
    6. Create the class level variable of the wpfusercontrol type
    WpfUserControlLib.UserControl1 _WPFUserControl = null;

     

    1. Add the following code to create the instance of the WPF user control and attach it with element host in the constructor or load event of the form.

     

                elementHost1.Dock = DockStyle.Fill;            _WPFUserControl = new WpfUserControlLib.UserControl1();            elementHost1.Child = _WPFUserControl;

    1. Make sure the form size is good enough to display the WPF control.

    Make sure WinFormWPFHostApp contains following references, though Visual Studio 2008 wocould add these by itself.

      • Windowsbase
      • Windowsformsintegration

      Compile and Run the Windows Application you wocould see the WPF control is hosted in the Windows Control.

       

      Summary:We can use the power of the WPF Controls visual elements into the WinForm application by hosting very easily. in the Next Article I wocould extend this to explain how to share the data and send the event to host from the WPF control.

      From [http://www.a2zdotnet.com/View.aspx? Id = 78 #. Ub8fGZyvy3I]

      Related Article

      Contact Us

      The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

      If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

      A Free Trial That Lets You Build Big!

      Start building with 50+ products and up to 12 months usage for Elastic Compute Service

      • Sales Support

        1 on 1 presale consultation

      • After-Sales Support

        24/7 Technical Support 6 Free Tickets per Quarter Faster Response

      • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.