Walkthrough: Hosting a Windows Forms Control in Windows Presentation Foundation

WPF provides many controls with a rich feature set. However, you may sometimes want to use Windows Forms controls on your WPF pages. For example, you may have a substantial investment in existing Windows Forms controls, or you may have a Windows Forms control that provides unique functionality.

This walkthrough shows you how to host a Windows Forms System.Windows.Forms.MaskedTextBox control on a WPF page by using code.

For a complete code listing of the tasks shown in this walkthrough, see Hosting a Windows Forms Control in Windows Presentation Foundation Sample.

NoteNote:

The dialog boxes and menu commands you see might differ from those described in Help, depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu.

Prerequisites

To complete this walkthrough you will need:

Hosting the Windows Forms Control

To host the MaskedTextBox control

  1. Create a Windows Application (WPF) project named HostingWfInWpf.

  2. In Solution Explorer, add a reference to the WindowsFormsIntegration assembly, which is named WindowsFormsIntegration.dll.

    The default location for this file is %programfiles%\Reference Assemblies\Microsoft\Framework\v3.0\WindowsFormsIntegration.dll.

  3. In Solution Explorer, add a reference to the Windows Forms assembly, which is named System.Windows.Forms.dll.

  4. In the Windows Forms Designer, open Window1.xaml.

  5. Replace the code in Window1.xaml with the following code.

    <Window x:Class="HostingWfInWpf.Window1"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        Title="HostingWfInWpf"
        Loaded="WindowLoaded"
        >
        <Grid Name="grid1">
    
        </Grid>
    </Window>
    
    <Window x:Class="Window1"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        Title="HostingWfInWpf" Height="300" Width="300"
        Loaded="WindowLoaded"
        >
      <Grid Name="grid1">
    
        </Grid>
    </Window>
    
  6. In the Code Editor, open Window1.xaml.cs.

  7. Replace the code in Window1.xaml.cs with the following code.

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Media;
    using System.Windows.Shapes;
    
    using System.Windows.Forms;
    
    namespace HostingWfInWpf
    {   
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
    
            private void WindowLoaded(object sender, RoutedEventArgs e) 
            {
                // Create the interop host control.
                System.Windows.Forms.Integration.WindowsFormsHost host =
                    new System.Windows.Forms.Integration.WindowsFormsHost();
    
                // Create the MaskedTextBox control.
                MaskedTextBox mtbDate = new MaskedTextBox("00/00/0000");
    
                // Assign the MaskedTextBox control as the host control's child.
                host.Child = mtbDate;
    
                // Add the interop host control to the Grid
                // control's collection of child controls.
                this.grid1.Children.Add(host);
            }
        }
    }
    
    Imports System
    Imports System.Windows
    Imports System.Windows.Controls
    Imports System.Windows.Data
    Imports System.Windows.Documents
    Imports System.Windows.Media
    Imports System.Windows.Media.Imaging
    Imports System.Windows.Shapes
    
    Imports System.Windows.Forms
    
    ' Interaction logic for Window1.xaml
    Partial Public Class Window1
        Inherits Window
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Private Sub WindowLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            ' Create the interop host control.
            Dim host As New System.Windows.Forms.Integration.WindowsFormsHost()
    
            ' Create the MaskedTextBox control.
            Dim mtbDate As New MaskedTextBox("00/00/0000")
    
            ' Assign the MaskedTextBox control as the host control's child.
            host.Child = mtbDate
    
            ' Add the interop host control to the Grid
            ' control's collection of child controls.
            Me.grid1.Children.Add(host)
    
        End Sub 'WindowLoaded
    
    End Class
    

See Also

Tasks

Walkthrough: Hosting a Windows Forms Control in Windows Presentation Foundation by Using XAML

Reference

ElementHost
WindowsFormsHost

Concepts

Walkthrough: Hosting a Windows Forms Composite Control in Windows Presentation Foundation
Walkthrough: Hosting a Windows Presentation Foundation Control in Windows Forms
Windows Forms Controls and Equivalent WPF Controls

Other Resources

Hosting a Windows Forms Control in Windows Presentation Foundation Sample
Migration and Interoperability How-to Topics