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

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 XAML.

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

Note   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. For more information, see Visual Studio Settings.

Prerequisites

You need the following components to complete this walkthrough:

  • Visual Studio 2008.

Hosting the Windows Forms Control

To host the MaskedTextBox control

  1. Create a WPF Application project named HostingWfInWpf.

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

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

  4. Open Window1.xaml in the WPF Designer.

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

    The wf namespace mapping establishes a reference to the DLL that contains the Windows Forms control. The MaskedTextBox control is created as a child of the WindowsFormsHost control.

    <Window x:Class="Window1"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"  
        Title="HostingWfInWpf"
        >
    
      <Grid>
    
        <WindowsFormsHost>
          <wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
        </WindowsFormsHost>
    
      </Grid>
    
    
    </Window>
    
    <Window x:Class="HostingWfInWpf.Window1"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"  
        Title="HostingWfInWpf"
        >
    
        <Grid>
    
            <WindowsFormsHost>
                <wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
            </WindowsFormsHost>
    
        </Grid>
    
    
    </Window>
    

See Also

Tasks

Walkthrough: Hosting a Windows Forms Control in Windows Presentation Foundation

Hosting a Windows Forms Control in Windows Presentation Foundation by Using XAML Sample

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

Reference

ElementHost

WindowsFormsHost

Other Resources

WPF Designer

Migration and Interoperability How-to Topics