* Links: .NET Desktop - framework\wpf * Apply suggestions from code review Co-authored-by: Andy De George <[email protected]>
4.8 KiB
title, titleSuffix, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | titleSuffix | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||
|---|---|---|---|---|---|---|---|---|---|
| Host 3D WPF Composite Control in Windows Forms | 08/18/2018 |
|
|
486369a9-606a-4a3b-b086-a06f2119c7b0 |
Walkthrough: Host a 3D WPF Composite Control in Windows Forms
This walkthrough demonstrates how you can create a [!INCLUDETLA2#tla_winclient] composite control and host it in Windows Forms controls and forms by using the xref:System.Windows.Forms.Integration.ElementHost control.
In this walkthrough, you will implement a [!INCLUDETLA2#tla_winclient] xref:System.Windows.Controls.UserControl that contains two child controls. The xref:System.Windows.Controls.UserControl displays a three-dimensional (3D) cone. Rendering 3D objects is much easier with the [!INCLUDETLA2#tla_winclient] than with Windows Forms. Therefore, it makes sense to host a [!INCLUDETLA2#tla_winclient] xref:System.Windows.Controls.UserControl class to create 3D graphics in Windows Forms.
Tasks illustrated in this walkthrough include:
-
Creating the [!INCLUDETLA2#tla_winclient] xref:System.Windows.Controls.UserControl.
-
Creating the Windows Forms host project.
-
Hosting the [!INCLUDETLA2#tla_winclient] xref:System.Windows.Controls.UserControl.
Prerequisites
You need the following components to complete this walkthrough:
- Visual Studio 2017
Create the UserControl
-
Create a WPF User Control Library project named
HostingWpfUserControlInWf. -
Open UserControl1.xaml in the WPF Designer.
-
Replace the generated code with the following code:
[!code-xamlHostingWpfUserControlInWf#1]
This code defines a xref:System.Windows.Controls.UserControl?displayProperty=nameWithType that contains two child controls. The first child control is a xref:System.Windows.Controls.Label?displayProperty=nameWithType control; the second is a xref:System.Windows.Controls.Viewport3D control that displays a 3D cone.
Create the host project
-
Add a Windows Forms App (.NET Framework) project named
WpfUserControlHostto the solution. -
In Solution Explorer, add a reference to the WindowsFormsIntegration assembly, which is named WindowsFormsIntegration.dll.
-
Add references to the following [!INCLUDETLA2#tla_winclient] assemblies:
-
PresentationCore
-
PresentationFramework
-
WindowsBase
-
-
Add a reference to the
HostingWpfUserControlInWfproject. -
In Solution Explorer, set the
WpfUserControlHostproject to be the startup project.
Host the UserControl
-
In the Windows Forms Designer, open Form1.
-
In the Properties window, click Events, and then double-click the xref:System.Windows.Forms.Form.Load event to create an event handler.
The Code Editor opens to the newly generated
Form1_Loadevent handler. -
Replace the code in Form1.cs with the following code.
The
Form1_Loadevent handler creates an instance ofUserControl1and adds itto the xref:System.Windows.Forms.Integration.ElementHost control's collection of child controls. The xref:System.Windows.Forms.Integration.ElementHost control is added to the form's collection of child controls.[!code-csharpHostingWpfUserControlInWf#10] [!code-vbHostingWpfUserControlInWf#10]
-
Press F5 to build and run the application.
See also
- xref:System.Windows.Forms.Integration.ElementHost
- xref:System.Windows.Forms.Integration.WindowsFormsHost
- Design XAML in Visual Studio
- Walkthrough: Hosting a WPF Composite Control in Windows Forms
- Walkthrough: Hosting a Windows Forms Composite Control in WPF
- Hosting a WPF Composite Control in Windows Forms Sample