Walkthrough: Hosting a Windows Forms User Control by Using the WPF Designer
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
This walkthrough shows you how to use the WPF Designer for Visual Studio to host a Windows Forms composite 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.
Creating the Windows Forms composite control.
Hosting the Windows Forms control in a WPF application.
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.
You need the following components to complete this walkthrough:
- Visual Studio 2012 RC.
Creating the Project
The first step is to create the project for the host application.
To create the project
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.
In Solution Explorer, add a reference to the WindowsFormsIntegration assembly, which is named WindowsFormsIntegration.dll.
In Solution Explorer, add a reference to the Windows Forms assembly, which is named System.Windows.Forms.dll.
Creating the Windows Forms Composite Control
This procedure shows how to create a composite control which sizes itself to its content. The TableLayoutPanel control and the AutoSize property provide the control’s dynamic layout capability. For more information, see TableLayoutPanel Control (Windows Forms).
To create the Windows Forms composite control
Add a new Windows Forms Control Library project named "DemoControlLibrary" to the solution. For more information, see How to: Add and Remove Solution Items.
UserControl1 opens in the Windows Forms Designer
Select Remove Last Row from the TableLayoutPanel Tasks smart tag menu.
In the Properties window, set the following properties for the Label control.
Click the smart tag glyph () on the MaskedTextBox control and select Set Mask from the MaskedTextBox Tasks menu.
The Input Mask dialog box appears.
From the Mask Description column, select Short date (or another predefined mask description) and click the OK button.
At the top of the Properties window, select the TableLayoutPanel control from the drop-down list.
Set the value of the AutoSize property to true.
At the top of the Properties window, select UserControl1.
UserControl1 resizes to fit its contents.
On the Build menu, select Build Solution to build the solution.
Hosting the Windows Forms Control in WPF
You use the WindowsFormsHost element to host UserControl1 in a WPF application.
To host the Windows Forms control in WPF
In the DemoApplication project of Solution Explorer, add a reference to the DemoControlLibrary project. For more information, see How to: Add or Remove References By Using the Add Reference Dialog Box.
Open MainWindow.xaml in the WPF Designer.
In XAML view, import the DemoControlLibrary namespace by inserting the following namespace mapping in the opening Window tag. For more information, see How to: Import a Namespace into XAML.
After the Grid opening tag, insert the following code.
<WindowsFormsHost HorizontalAlignment="Left" VerticalAlignment="Top"> <dc:UserControl1/> </WindowsFormsHost>
A region representing the hosted Windows Forms control appears in the designer.
Run the solution to test the user control.
You can also host WPF controls in Windows Forms. For more information, see Using WPF Controls.