Files
docs-desktop/dotnet-desktop-guide/framework/wpf/advanced/walkthrough-hosting-a-3-d-wpf-composite-control-in-windows-forms.md
T
Andy De George da363692ff Initial WPF content migrated (#17)
* Reset branch for WPF changes

* Convert BMP to PNG; fix link-out-of-scope err

* Add snippets for WPF... 6794 files!!!!

* Add missing snippets

* update file updated between migration

* Fix paths to include

* update breadcrumb and toc

* fix index links

* fix index links

* fix index links

* fix markdown
2020-09-04 09:46:28 -07:00

4.7 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
csharp
vb
hosting WPF content in Windows Forms [WPF]
composite controls [WPF], hosting WPF in
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:

Prerequisites

You need the following components to complete this walkthrough:

  • Visual Studio 2017

Create the UserControl

  1. Create a WPF User Control Library project named HostingWpfUserControlInWf.

  2. Open UserControl1.xaml in the WPF Designer.

  3. 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

  1. Add a Windows Forms App (.NET Framework) project named WpfUserControlHost to the solution.

  2. In Solution Explorer, add a reference to the WindowsFormsIntegration assembly, which is named WindowsFormsIntegration.dll.

  3. Add references to the following [!INCLUDETLA2#tla_winclient] assemblies:

    • PresentationCore

    • PresentationFramework

    • WindowsBase

  4. Add a reference to the HostingWpfUserControlInWf project.

  5. In Solution Explorer, set the WpfUserControlHost project to be the startup project.

Host the UserControl

  1. In the Windows Forms Designer, open Form1.

  2. 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_Load event handler.

  3. Replace the code in Form1.cs with the following code.

    The Form1_Load event handler creates an instance of UserControl1 and 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]

  4. Press F5 to build and run the application.

See also