Files
docs-desktop/dotnet-desktop-guide/framework/wpf/advanced/walkthrough-mapping-properties-using-the-elementhost-control.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

8.5 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
Walkthrough: Mapping Properties Using the ElementHost Control 08/18/2018
csharp
vb
mapping properties [WPF]
ElementHost control [WPF], mapping properties
bccd6e0d-2272-4924-9107-ff8ed58b88aa

Walkthrough: Mapping Properties Using the ElementHost Control

This walkthrough shows you how to use the xref:System.Windows.Forms.Integration.ElementHost.PropertyMap%2A property to map Windows Forms properties to corresponding properties on a hosted [!INCLUDETLA2#tla_winclient] element.

Tasks illustrated in this walkthrough include:

  • Creating the project.

  • Defining a new property mapping.

  • Removing a default property mapping.

  • Extending a default property mapping.

For a complete code listing of the tasks illustrated in this walkthrough, see Mapping Properties Using the ElementHost Control Sample.

When you are finished, you will be able to map Windows Forms properties to corresponding [!INCLUDETLA2#tla_winclient] properties on a hosted element.

Prerequisites

You need the following components to complete this walkthrough:

  • Visual Studio 2017

Creating the Project

To create the project

  1. Create a Windows Forms App project named PropertyMappingWithElementHost.

  2. In Solution Explorer, add references to the following [!INCLUDETLA2#tla_winclient] assemblies.

    • PresentationCore

    • PresentationFramework

    • WindowsBase

    • WindowsFormsIntegration

  3. Copy the following code into the top of the Form1 code file.

    [!code-csharpPropertyMappingWithElementHost#10] [!code-vbPropertyMappingWithElementHost#10]

  4. Open Form1 in the Windows Forms Designer. Double-click the form to add an event handler for the xref:System.Windows.Forms.Form.Load event.

  5. Return to the Windows Forms Designer and add an event handler for the form's xref:System.Windows.Forms.Control.Resize event. For more information, see How to: Create Event Handlers Using the Designer.

  6. Declare an xref:System.Windows.Forms.Integration.ElementHost field in the Form1 class.

    [!code-csharpPropertyMappingWithElementHost#16] [!code-vbPropertyMappingWithElementHost#16]

Defining New Property Mappings

The xref:System.Windows.Forms.Integration.ElementHost control provides several default property mappings. You add a new property mapping by calling the xref:System.Windows.Forms.Integration.PropertyMap.Add%2A method on the xref:System.Windows.Forms.Integration.ElementHost control's xref:System.Windows.Forms.Integration.ElementHost.PropertyMap%2A.

To define new property mappings

  1. Copy the following code into the definition for the Form1 class.

    [!code-csharpPropertyMappingWithElementHost#12] [!code-vbPropertyMappingWithElementHost#12]

    The AddMarginMapping method adds a new mapping for the xref:System.Windows.Forms.Control.Margin%2A property.

    The OnMarginChange method translates the xref:System.Windows.Forms.Control.Margin%2A property to the [!INCLUDETLA2#tla_winclient] xref:System.Windows.FrameworkElement.Margin%2A property.

  2. Copy the following code into the definition for the Form1 class.

    [!code-csharpPropertyMappingWithElementHost#14] [!code-vbPropertyMappingWithElementHost#14]

    The AddRegionMapping method adds a new mapping for the xref:System.Windows.Forms.Control.Region%2A property.

    The OnRegionChange method translates the xref:System.Windows.Forms.Control.Region%2A property to the [!INCLUDETLA2#tla_winclient] xref:System.Windows.UIElement.Clip%2A property.

    The Form1_Resize method handles the form's xref:System.Windows.Forms.Control.Resize event and sizes the clipping region to fit the hosted element.

Removing a Default Property Mapping

Remove a default property mapping by calling the xref:System.Windows.Forms.Integration.PropertyMap.Remove%2A method on the xref:System.Windows.Forms.Integration.ElementHost control's xref:System.Windows.Forms.Integration.ElementHost.PropertyMap%2A.

To remove a default property mapping

Extending a Default Property Mapping

You can use a default property mapping and also extend it with your own mapping.

To extend a default property mapping

Initialize your property mappings

  1. Copy the following code into the definition for the Form1 class.

    [!code-csharpPropertyMappingWithElementHost#11] [!code-vbPropertyMappingWithElementHost#11]

    The Form1_Load method handles the xref:System.Windows.Forms.Form.Load event and performs the following initialization.

  2. Press F5 to build and run the application.

See also