This topic shows you how to create a Windows Presentation Foundation (WPF) control for use in your Windows Forms-based applications.
In this walkthrough, you perform the following tasks:
Create the project.
Create a new WPF control.
Add the new WPF control to a Windows Form. The WPF control is hosted in an ElementHost control.
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 2008.
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 a New WPF Control
Creating a new WPF control and adding it to your project is as easy as adding any other item to your project. The Windows Forms Designer works with a particular kind of control named composite control, or user control. For more information about WPF user controls, see UserControl.
To create a new WPF control
In Solution Explorer, add a new WPF User Control Library project to the solution. Use the default name for the control library,
WpfControlLibrary1. The default control name is
Adding the new control has the following effects.
File UserControl1.xaml is added.
Either file UserControl1.xaml.cs or UserControl1.xaml.vb is added. This file contains the code-behind for event handlers and other implementation.
References to WPF assemblies are added.
File UserControl1.xaml opens in the WPF Designer for Visual Studio.
In Design view, make sure that
UserControl1is selected. For more information, see How to: Select and Move Elements on the Design Surface.
From the Toolbox, drag a TextBox control onto the design surface.
In the Properties window, set the value of the Text property to Hosted Content.
In general, you should host more sophisticated WPF content. The TextBox control is used here for illustrative purposes only.
Build the project.
Adding a WPF Control to a Windows Form
Your new WPF control is ready for use on the form. Windows Forms uses the ElementHost control to host WPF content
To add a WPF control to a Windows Form
Form1in the Windows Forms Designer.
In the Toolbox, find the tab labeled WPFUserControlLibrary WPF User Controls.
Drag an instance of
UserControl1onto the form.
An ElementHost control is created automatically on the form to host the WPF control.
References to WPF assemblies are added to the project.
elementHost1control has a smart tag panel that shows the available hosting options.
In the ElementHost Tasks smart tag panel, select Dock in parent container.
Press F5 to build and run the application.
Windows Forms and WPF are different technologies, but they are designed to interoperate closely. To provide richer appearance and behavior in your applications, try the following.
Host a Windows Forms control in a WPF page. For more information, see Walkthrough: Hosting a Windows Forms Control in WPF.
Apply Windows Forms visual styles to your WPF content. For more information, see How to: Enable Visual Styles in a Hybrid Application.
Change the style of your WPF content. For more information, see Walkthrough: Styling WPF Content.