Walkthrough: Assigning WPF Content on Windows Forms at Design Time
This walkthrough show you how to select the Windows Presentation Foundation (WPF) control types you want to display on your form. You can select any WPF control types which are included in your project.
In this walkthrough, you perform the following tasks:
Create the project.
Create the WPF control types.
Select WPF controls.
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 Customizing Development Settings in Visual Studio.
You need the following components to complete this walkthrough:
- Visual Studio 2012.
Creating the Project
The first step is to create the Windows Forms project.
When hosting WPF content, only C# and Visual Basic projects are supported.
To create the project
- Create a new Windows Forms Application project in Visual Basic or Visual C# named
Creating the WPF Control Types
After you add WPF control types to the project, you can host them in different ElementHost controls.
To create WPF control types
Add a new WPF UserControl project to the solution. Use the default name for the control type,
UserControl1.xaml. For more information, see Walkthrough: Creating New WPF Content on Windows Forms at Design Time.
In Design view, make sure that
UserControl1is selected. For more information, see How to: Select and Move Elements on the Design Surface.
Add a second WPF UserControl to the project. Use the default name for the control type,
Note In general, you should host more sophisticated WPF content. The System.Windows.Controls.TextBox control is used here for illustrative purposes only.
- Build the project.
Selecting WPF Controls
You can assign different WPF content to an ElementHost control, which is already hosting content.
To select WPF controls
Form1in the Windows Forms Designer.
In the Toolbox, double-click
UserControl1to create an instance of
UserControl1on the form.
An instance of
UserControl1is hosted in a new ElementHost control named
In the smart tag panel for
elementHost1, open the Select Hosted Content drop-down list.
Select UserControl2 from the drop-down list box.
elementHost1control now hosts an instance of the
In the Properties window, confirm that the Child property is set to UserControl2.
From the Toolbox, in the WPF Interoperability group, drag an ElementHost control onto the form.
The default name for the new control is
In the smart tag panel for
elementHost2, open the Select Hosted Content drop-down list.
Select UserControl1 from the drop-down list.
elementHost2control now hosts an instance of the