* 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
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 |
|
|
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
-
Create a Windows Forms App project named
PropertyMappingWithElementHost. -
In Solution Explorer, add references to the following [!INCLUDETLA2#tla_winclient] assemblies.
-
PresentationCore
-
PresentationFramework
-
WindowsBase
-
WindowsFormsIntegration
-
-
Copy the following code into the top of the
Form1code file.[!code-csharpPropertyMappingWithElementHost#10] [!code-vbPropertyMappingWithElementHost#10]
-
Open
Form1in the Windows Forms Designer. Double-click the form to add an event handler for the xref:System.Windows.Forms.Form.Load event. -
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.
-
Declare an xref:System.Windows.Forms.Integration.ElementHost field in the
Form1class.[!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
-
Copy the following code into the definition for the
Form1class.[!code-csharpPropertyMappingWithElementHost#12] [!code-vbPropertyMappingWithElementHost#12]
The
AddMarginMappingmethod adds a new mapping for the xref:System.Windows.Forms.Control.Margin%2A property.The
OnMarginChangemethod translates the xref:System.Windows.Forms.Control.Margin%2A property to the [!INCLUDETLA2#tla_winclient] xref:System.Windows.FrameworkElement.Margin%2A property. -
Copy the following code into the definition for the
Form1class.[!code-csharpPropertyMappingWithElementHost#14] [!code-vbPropertyMappingWithElementHost#14]
The
AddRegionMappingmethod adds a new mapping for the xref:System.Windows.Forms.Control.Region%2A property.The
OnRegionChangemethod translates the xref:System.Windows.Forms.Control.Region%2A property to the [!INCLUDETLA2#tla_winclient] xref:System.Windows.UIElement.Clip%2A property.The
Form1_Resizemethod 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
-
Copy the following code into the definition for the
Form1class.[!code-csharpPropertyMappingWithElementHost#13] [!code-vbPropertyMappingWithElementHost#13]
The
RemoveCursorMappingmethod deletes the default mapping for the xref:System.Windows.Forms.Control.Cursor%2A property.
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
-
Copy the following code into the definition for the
Form1class.[!code-csharpPropertyMappingWithElementHost#15] [!code-vbPropertyMappingWithElementHost#15]
The
ExtendBackColorMappingmethod adds a custom property translator to the existing xref:System.Windows.Forms.Control.BackColor%2A property mapping.The
OnBackColorChangemethod assigns a specific image to the hosted control's xref:System.Windows.Controls.Control.Background%2A property. TheOnBackColorChangemethod is called after the default property mapping is applied.
Initialize your property mappings
-
Copy the following code into the definition for the
Form1class.[!code-csharpPropertyMappingWithElementHost#11] [!code-vbPropertyMappingWithElementHost#11]
The
Form1_Loadmethod handles the xref:System.Windows.Forms.Form.Load event and performs the following initialization.-
Creates a [!INCLUDETLA2#tla_winclient] xref:System.Windows.Controls.Button element.
-
Calls the methods you defined earlier in the walkthrough to set up the property mappings.
-
Assigns initial values to the mapped properties.
-
-
Press F5 to build and run the application.
See also
- xref:System.Windows.Forms.Integration.ElementHost.PropertyMap%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.Integration.WindowsFormsHost.PropertyMap%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.Integration.WindowsFormsHost
- Windows Forms and WPF Property Mapping
- Design XAML in Visual Studio
- Walkthrough: Hosting a WPF Composite Control in Windows Forms