How can I Host the WPF UserControl in Windows Forms ?

Platform: WPF| Category: WPF in Win32

This can be done with the help of the following code :

[C#[

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
namespace WpfUserControlHost
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // Create the ElementHost control for hosting the
            // WPF UserControl.
            ElementHost host = new ElementHost();
            host.Dock = DockStyle.Fill;

            // Create the WPF UserControl.
            HostingWpfUserControlInWf.UserControl1 uc =
                new HostingWpfUserControlInWf.UserControl1();
            // Assign the WPF UserControl to the ElementHost control’s
            // Child property.
            host.Child = uc;
            // Add the ElementHost control to the form’s
            // collection of child controls.
            this.Controls.Add(host);
        }
    }
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.