This documentation is archived and is not being maintained.

How to: Host a Windows Presentation Foundation Control in Windows Forms by Using ElementHost

The ElementHost class allows you to host a Windows Presentation Foundation (WPF) control in Windows Forms. When you host the control, you can receive events and access the control's exposed properties. The simplest way to host a WPF control is to add a Panel control to your form, and then host the WPF control in the Panel. You typically do this in your form's Load event handler.

The following code example demonstrates how to use the ElementHost control to host a WPF control in Windows Forms. It also shows how to attach handlers to two of the control's custom OnButtonClick events. See Hosting a Windows Presentation Foundation Control in Windows Forms Sample for the complete sample.

NoteNote:

The hosted control is not fully initialized at the time of the form's Load event. The control's Loaded event, which occurs later, indicates that the control has been initialized. The example attaches a handler to the control's Loaded event because it needs to acquire the initial values of several properties that are not defined at the time of the form's Load event.

Example

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using System.Windows.Media;

namespace WFHost
{
    partial class Form1 : Form
    {
        private ElementHost ctrlHost;
        private MyControls.Page1 avAddressCtrl;
        System.Windows.FontWeight initFontWeight;
        double initFontSize;
        System.Windows.FontStyle initFontStyle;
        System.Windows.Media.SolidColorBrush initBackBrush;
        System.Windows.Media.SolidColorBrush initForeBrush;
        FontFamily initFontFamily;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ctrlHost = new ElementHost();
            ctrlHost.Dock = DockStyle.Fill;
            panel1.Controls.Add(ctrlHost);
            avAddressCtrl = new MyControls.Page1();
            avAddressCtrl.InitializeComponent();
            ctrlHost.Child = avAddressCtrl;

            avAddressCtrl.OnButtonClick += 
                new MyControls.Page1.MyControlEventHandler(
                avAddressCtrl_OnButtonClick);
            avAddressCtrl.Loaded += new RoutedEventHandler(
                avAddressCtrl_Loaded);
        }

        void avAddressCtrl_Loaded(object sender, EventArgs e)
        {
            initBackBrush = (SolidColorBrush)avAddressCtrl.MyControl_Background;
            initForeBrush = avAddressCtrl.MyControl_Foreground;
            initFontFamily = avAddressCtrl.MyControl_FontFamily;
            initFontSize = avAddressCtrl.MyControl_FontSize;
            initFontWeight = avAddressCtrl.MyControl_FontWeight;
            initFontStyle = avAddressCtrl.MyControl_FontStyle;
        }

        void avAddressCtrl_OnButtonClick(
            object sender, 
            MyControls.MyControlEventArgs args)
        {
            if (args.IsOK)
            {
                lblAddress.Text = "Street Address: " + args.MyStreetAddress;
                lblCity.Text = "City: " + args.MyCity;
                lblName.Text = "Name: " + args.MyName;
                lblState.Text = "State: " + args.MyState;
                lblZip.Text = "Zip: " + args.MyZip;
            }
            else
            {
                lblAddress.Text = "Street Address: ";
                lblCity.Text = "City: ";
                lblName.Text = "Name: ";
                lblState.Text = "State: ";
                lblZip.Text = "Zip: ";
            }
        }

        private void radioBackgroundOriginal_CheckedChanged(object sender, EventArgs e)
        {
            avAddressCtrl.MyControl_Background = initBackBrush;
        }

        private void radioBackgroundLightGreen_CheckedChanged(object sender, EventArgs e)
        {
            avAddressCtrl.MyControl_Background = new SolidColorBrush(Colors.LightGreen);
        }

        private void radioBackgroundLightSalmon_CheckedChanged(object sender, EventArgs e)
        {
            avAddressCtrl.MyControl_Background = new SolidColorBrush(Colors.LightSalmon);
        }

        private void radioForegroundOriginal_CheckedChanged(object sender, EventArgs e)
        {
            avAddressCtrl.MyControl_Foreground = initForeBrush;
        }

        private void radioForegroundRed_CheckedChanged(object sender, EventArgs e)
        {
            avAddressCtrl.MyControl_Foreground = new System.Windows.Media.SolidColorBrush(Colors.Red);
        }

        private void radioForegroundYellow_CheckedChanged(object sender, EventArgs e)
        {
            avAddressCtrl.MyControl_Foreground = new System.Windows.Media.SolidColorBrush(Colors.Yellow);
        }

        private void radioFamilyOriginal_CheckedChanged(object sender, EventArgs e)
        {
            avAddressCtrl.MyControl_FontFamily = initFontFamily;
        }

        private void radioFamilyTimes_CheckedChanged(object sender, EventArgs e)
        {
            avAddressCtrl.MyControl_FontFamily = new FontFamily("Times New Roman");
        }

        private void radioFamilyWingDings_CheckedChanged(object sender, EventArgs e)
        {
            avAddressCtrl.MyControl_FontFamily = new FontFamily("WingDings");
        }

        private void radioSizeOriginal_CheckedChanged(object sender, EventArgs e)
        {
            avAddressCtrl.MyControl_FontSize = initFontSize;
        }

        private void radioSizeTen_CheckedChanged(object sender, EventArgs e)
        {
            avAddressCtrl.MyControl_FontSize = 10;
        }

        private void radioSizeTwelve_CheckedChanged(object sender, EventArgs e)
        {
            avAddressCtrl.MyControl_FontSize = 12;
        }

        private void radioStyleOriginal_CheckedChanged(object sender, EventArgs e)
        {
            avAddressCtrl.MyControl_FontStyle = initFontStyle;
        }

        private void radioStyleItalic_CheckedChanged(object sender, EventArgs e)
        {
            avAddressCtrl.MyControl_FontStyle = System.Windows.FontStyles.Italic;
        }

        private void radioWeightOriginal_CheckedChanged(object sender, EventArgs e)
        {
            avAddressCtrl.MyControl_FontWeight = initFontWeight;
        }

        private void radioWeightBold_CheckedChanged(object sender, EventArgs e)
        {
            avAddressCtrl.MyControl_FontWeight = FontWeights.Bold;
        }
    }
}

See Also

Show: