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

9.1 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
Walkthrough: Mapping Properties Using the WindowsFormsHost Element 08/18/2018
csharp
vb
mapping properties [WPF]
WindowsFormsHost element property mapping [WPF]
74809167-bf8e-48b7-a2e7-b4ea08bc7d8c

Walkthrough: Mapping Properties Using the WindowsFormsHost Element

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

Tasks illustrated in this walkthrough include:

  • Creating the project.

  • Defining the application layout.

  • Defining a new property mapping.

  • Removing a default property mapping.

  • Replacing 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 WindowsFormsHost Element Sample.

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

Prerequisites

You need the following components to complete this walkthrough:

  • Visual Studio 2017

Create and set up the project

  1. Create a WPF App project named PropertyMappingWithWfhSample.

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

  3. In Solution Explorer, add references to the System.Drawing and System.Windows.Forms assemblies.

Defining the Application Layout

The [!INCLUDETLA2#tla_winclient]-based application uses the xref:System.Windows.Forms.Integration.WindowsFormsHost element to host a Windows Forms control.

To define the application layout

  1. Open Window1.xaml in the WPF Designer.

  2. Replace the existing code with the following code.

    [!code-xamlPropertyMappingWithWfhSample#1]

  3. Open Window1.xaml.cs in the Code Editor.

  4. At the top of the file, import the following namespaces.

    [!code-csharpPropertyMappingWithWfhSample#20] [!code-vbPropertyMappingWithWfhSample#20]

Defining a New Property Mapping

The xref:System.Windows.Forms.Integration.WindowsFormsHost element 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.WindowsFormsHost element's xref:System.Windows.Forms.Integration.WindowsFormsHost.PropertyMap%2A.

To define a new property mapping

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.WindowsFormsHost element's xref:System.Windows.Forms.Integration.WindowsFormsHost.PropertyMap%2A.

To remove a default property mapping

Replacing a Default Property Mapping

Replace a default property mapping by removing the default mapping and calling the xref:System.Windows.Forms.Integration.PropertyMap.Add%2A method on the xref:System.Windows.Forms.Integration.WindowsFormsHost element's xref:System.Windows.Forms.Integration.WindowsFormsHost.PropertyMap%2A.

To replace 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

Initializing Your Property Mappings

Set up your property mappings by calling the previously described methods in the xref:System.Windows.FrameworkElement.Loaded event handler.

To initialize your property mappings

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

    [!code-csharpPropertyMappingWithWfhSample#11] [!code-vbPropertyMappingWithWfhSample#11]

    The WindowLoaded method handles the xref:System.Windows.FrameworkElement.Loaded event and performs the following initialization.

    • Creates a Windows Formsxref:System.Windows.Forms.CheckBox control.

    • Calls the methods you defined earlier in the walkthrough to set up the property mappings.

    • Assigns initial values to the mapped properties.

  2. Press F5 to build and run the application. Click the check box to see the effect of the xref:System.Windows.FrameworkElement.FlowDirection%2A mapping. When you click the check box, the layout reverses its left-right orientation.

See also