Udostępnij za pośrednictwem


Walkthrough: Hosting a Windows Forms Control in a WPF Application

This walkthrough shows you how to use the WPF Designer for Visual Studio to host a Windows Forms control in a WPF application. For more information about Windows Forms and WPF interoperability, see Migration and Interoperability.

Tasks explained in this walkthrough include:

  • Creating the project.

  • Hosting the Windows Forms control in a WPF application.

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 Working with Settings.

Prerequisites

You need the following components to complete this walkthrough:

  • Visual Studio 2010.

Creating the Project

The first step is to create the project for the host application.

To create the project

  1. Create a WPF Application project named "DemoApplication". For more information, see How to: Create a New WPF Application Project.

    MainWindow.xaml opens in the WPF Designer.

  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.

Hosting the Windows Forms Control in WPF

You use the WindowsFormsHost element to host a Windows Forms control in a WPF application.

To host the Windows Forms Control in WPF

  1. In XAML view for MainWindow.xaml, import the Windows Forms namespace by inserting the following namespace mapping in the opening Window tag. For more information, see How to: Import a Namespace into XAML.

    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    
  2. After the Grid opening tag, insert the following code.

    <WindowsFormsHost>
        <wf:MaskedTextBox Mask="00/00/0000" />
    </WindowsFormsHost>
    

    A region representing the hosted Windows Forms control (MaskedTextBox) appears in the designer.

Next Steps

You can also host WPF controls in Windows Forms. For more information, see Using WPF Controls.

See Also

Tasks

Walkthrough: Hosting a Windows Forms User Control by Using the WPF Designer

Walkthrough: Hosting a Windows Forms Control in WPF

Reference

WindowsFormsHost

ElementHost

Other Resources

Working with Controls in the WPF Designer

Migration and Interoperability