Walkthrough: Assign WPF content on Windows Forms at design time
This article 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 that are included in your project.
Prerequisites
You need Visual Studio to complete this walkthrough.
Create the project
Open Visual Studio and create a new Windows Forms Application project in Visual Basic or Visual C# named SelectingWpfContent
.
Note
When hosting WPF content, only C# and Visual Basic projects are supported.
Create the WPF control types
After you add WPF control types to the project, you can host them in different ElementHost controls.
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
UserControl1
is selected.In the Properties window, set the value of the Width and Height properties to 200.
Add a System.Windows.Controls.TextBox control to the UserControl and set the value of the Text property to Hosted Content.
Add a second WPF UserControl to the project. Use the default name for the control type,
UserControl2.xaml
.In the Properties window, set the value of the Width and Height properties to 200.
Add a System.Windows.Controls.TextBox control to the UserControl and set the value of the Text property to Hosted Content 2.
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.
Select WPF controls
You can assign different WPF content to an ElementHost control, which is already hosting content.
Open
Form1
in the Windows Forms Designer.In the Toolbox, double-click
UserControl1
to create an instance ofUserControl1
on the form.An instance of
UserControl1
is hosted in a new ElementHost control namedelementHost1
.In the smart tag panel for
elementHost1
, open the Select Hosted Content drop-down list.Select UserControl2 from the drop-down list box.
The
elementHost1
control now hosts an instance of theUserControl2
type.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
elementHost2
.In the smart tag panel for
elementHost2
, open the Select Hosted Content drop-down list.Select UserControl1 from the drop-down list.
The
elementHost2
control now hosts an instance of theUserControl1
type.
See also
.NET Desktop feedback