Walkthrough: Hosting a Windows Forms User Control by Using the WPF Designer
This walkthrough shows you how to use the Windows Presentation Foundation (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.
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.
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.
Window1.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
From the Toolbox, double-click the TableLayoutPanel control. A TableLayoutPanel control appears on the design surface.
Select Remove Last Row from the TableLayoutPanel Tasks smart tag menu.
In the Properties window, set the value of the Dock property to Fill.
From the Toolbox, add a Label control to the first cell of the TableLayoutPanel control.
In the Properties window, set the following properties for the Label control.
Property
Value
AutoSize
True
Dock
Fill
Margin
0,0,0,0
Text
Enter Date:
TextAlign
MiddleCenter
From the Toolbox, add a MaskedTextBox control into the second cell of the TableLayoutPanel 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.
Set the value of the AutoSize property to true, and set the value of the Size property to 0,0.
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 in Visual Studio (Visual Basic).
Open Window1.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.
xmlns:dc="clr-namespace:DemoControlLibrary;assembly=DemoControlLibrary"
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.
Next Steps
You can also host WPF controls in Windows Forms. For more information, see Using Windows Presentation Foundation Controls.
See Also
Tasks
Walkthrough: Hosting a Windows Forms Control in Windows Presentation Foundation